一、@RequestMapping 修饰
1、不仅可以修饰方法还可以修饰类
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
private static final String SUCCESS = "success";
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
System.out.println("testRequestMapping");
return SUCCESS;
}
}
@RequestMapping中的值为访问时的路径(类在方法前)
二、@RequestMapping 映射请求
1、 value, method;
value: 指定请求的实际地址;
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
4、Ant风格的通配符
a)-? 匹配文件名的一个字符
b) -* 匹配文件名中的任意字符
c)-** 匹配多层路径
三、实例
@RequestMapping(value="/testMethod",method=RequestMethod.POST)
public String testMethod(){
System.out.println("testMethod");
return SUCCESS;
}
@RequestMapping("/testAntPath/*/abc")
public String testAntPath(){
System.out.println("testAntPath");
return SUCCESS;
}
四、@PathVariable
可以映射URL中的占位符到目标方法的参数中
@RequestMapping(value="/testPathVariable/{id}")
public String testPathVariable(@PathVariable(value="id") Integer id){
System.out.println("testPathVariable:"+id);
return SUCCESS;
}
五、REST
四个表示操作方式的动词:GET、POST、PUT、DELETE。分别代表
获取、新建资源、更新资源、删除资源
使用 PUT和DELETE需要配置web.xml
<!--
配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求
-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
实例:
index.jsp
<a href="springmvc/testRest/1">Test Rest GET</a>
<form action="springmvc/testRest" method="POST">
<input type="submit" value="Test Rest POST">
</form>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="TestRest PUT"/>
</form>
<br><br>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="TestRest DELETE"/>
</form>
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
private static final String SUCCESS = "success";
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
@ResponseBody()
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRest Put: " + id);
return SUCCESS;
}
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
@ResponseBody()
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRest Delete: " + id);
return SUCCESS;
}
@RequestMapping(value="/testRest",method=RequestMethod.POST)
public String testRest(){
System.out.println("POST:");
return SUCCESS;
}
@RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
public String testRest(@PathVariable(value="id") Integer id){
System.out.println("GET:"+id);
return SUCCESS;
}
}