Spring MVC @PathVariable注解

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88794863

简介

@PathVariable 映射 URL 绑定的占位符。带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义。

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。主要是根据请求方法进行类的区别。

例如:

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
   <a href="helloworld_mvc/testPathVariable/1004">PathVariable</a>
  </body>
</html>

HelloWorld.java

package com.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/helloworld_mvc")
public class HelloWorld {

    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(value = "id") Integer id){
        return "success";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88794863