SpringMVC中@RequestMapping注解用法实例
1 修饰类和方法
package site.exciter.springMVC.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.WEB.bind.annotation.RequestMapping;
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping");
return "success";
}
}
2 value
@RequestMapping("/springmvc")
可以改为@RequestMapping(value = "springmvc")
3 method
post请求
controller
@RequestMapping(value = "testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("testMethod");
return "success";
}
jsp
<fORM action="/springmvc/testMethod" method="post">
<input type="submit" value="submit">
</form>
4 params和headers
@RequestMapping(value = "testPramsAndHeaders",
params = {"username", "age!=10"}, headers = {})
public String testPramsAndHeaders() {
System.out.println("testPramsAndHeaders");
return "success";
}
<a href="/springmvc/testPramsAndHeaders?username=exciter&age=10" rel="external nofollow" >testPramsAndHeaders</a>
当age=10时,访问会出错;当age为其他值时,正常访问。
http://localhost:8080/springmvc/testPramsAndHeaders?username=exciter&age=10
5 Ant路径
@RequestMapping("/testAntPath
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("testPathVariable:" + id);
return "success";
}
<a href="/springmvc/testPathVariable/1" rel="external nofollow" >testPathVariable</a>
6 HiddenHttpMethodFilter
HiddenHttpMethodFilter:浏览器 form 表单只支持 GET、POST、HEAD 请求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与DELETE 请求。
web.xml中配置HiddenHttpMethodFilter
<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter,可以把 POST 请求转为 DELETE 或 PUT 请求 -->
<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>
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRestDelete:" + id);
return "success";
}
<a href="/springmvc/testRest/1" rel="external nofollow" >
<button>TestRest GET</button>
</a>
<form action="/springmvc/testRest" method="post">
<input type="submit" value="TestRest POST">
</form>
<form action="/springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="TestRest PUT">
</form>
<form action="/springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="TestRest DELETE">
</form>
注意: 在Tomcat8.0以上请求完接口之后返回页会报错:
HTTP状态 405 - 方法不允许
类型 状态报告消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS
描述 请求行中接收的方法由源服务器知道,但目标资源不支持
临时解决方案:在返回的jsp中添加isErrorPage="true"
:
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
7 @RequestParam
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam(value = "username") String username,
@RequestParam(value = "age", required = false, defaultValue = "0") int age) {
System.out.println("testRequestParam: username=" + username + " age=" + age);
return "success";
}
<a href="/springmvc/testRequestParam?username=exciter&age=20" rel="external nofollow" rel="external nofollow" >testRequestParam</a>
8 @RequestHeader
@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value = "Accept-Language") String al) {
System.out.println("testRequestHeader:" + al);
return "success";
}
<a href="/springmvc/testRequestParam?username=exciter&age=20" rel="external nofollow" rel="external nofollow" >testRequestParam</a>
9 @CookieValue
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value = "JSESSIONID") String sessionId) {
System.out.println("testCookieValue:" + sessionId);
return "success";
}
总结
到此这篇关于SpringMVC中@RequestMapping注解用法的文章就介绍到这了,更多相关SpringMVC @RequestMapping用法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章