SpringMVC @RequestMapping用法
木水Code 人气:01 修饰类和方法
package site.exciter.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/springmvc") @Controller public class SpringMVCTest { /** * 1. @RequestMapping 除了修饰方法, 还可来修饰类 * 2. * 1). 类定义处: 提供初步的请求映射信息。相对于 WEB 应用的根目录 * 2). 方法处: 提供进一步的细分映射信息。 相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL * 相对于 WEB 应用的根目录 */ @RequestMapping("/testRequestMapping") public String testRequestMapping() { System.out.println("testRequestMapping"); return "success"; } }
2 value
@RequestMapping("/springmvc")
可以改为@RequestMapping(value = "springmvc")
3 method
post请求
controller
/** * method比较常用,用来指定请求方式 * @return */ @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
/** * 可以使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式. * * @return */ @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路径
/** * 通配符 Ant风格,*可以是任何内容 * * @return */ @RequestMapping("/testAntPath/*/exciter") public String testAndPath() { System.out.println("testAndPath"); return "success"; }
<a href="/springmvc/testAntPath/ww/exciter" rel="external nofollow" >testAntPath</a>
5 @PathVariable
/** * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中 */ @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>/*</url-pattern> </filter-mapping>
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET) public String testRestGet(@PathVariable("id") Integer id) { System.out.println("testRestGet:" + id); return "success"; } @RequestMapping(value = "/testRest", method = RequestMethod.POST) public String testRestPost() { System.out.println("testRestPost"); return "success"; } @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT) public String testRestPut(@PathVariable Integer id) { System.out.println("testRestPut:" + id); return "success"; } /** * Rest 风格的 URL. 以 CRUD 为例: 新增: /order POST 修改: /order/1 PUT update?id=1 获取: * /order/1 GET get?id=1 删除: /order/1 DELETE delete?id=1 * <p> * 如何发送 PUT 请求和 DELETE 请求呢 ? 1. 需要配置 HiddenHttpMethodFilter 2. 需要发送 POST 请求 * 3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT * <p> * 在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解 */ @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
/** * @RequestParam 来映射请求参数 * value 值即请求参数的参数名 * required 该参数是否必须. 默认为 true * defaultValue 请求参数的默认值 */ @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
/** * 映射请求头信息 用法同 @RequestParam */ @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"; }
总结
加载全部内容