Spring mvc中控制器类的注解为 @RequestMapping,requestmapping注解参数

  Spring mvc中控制器类的注解为 @RequestMapping,requestmapping注解参数

  

目录

1 修饰类和方法注射毒品值3方法4参数和标题5只蚂蚁路径5 @路径变量6 hiddenhttpmethodfilter 7 @请求参数8 @请求头9 @ CookieValue总结

 

  

1 修饰类和方法

包网站。励磁机。春天MVC。经手人;导入org。spring框架。刻板印象。控制器;导入org。spring框架。网络。绑定。注释。请求映射;@请求映射(/spring MVC )@控制器公共类spring MVC test {/* * * 1 .@RequestMapping除了修饰方法,还可来修饰类* 2.* 1).类定义处: 提供初步的请求映射信息。相对于网应用的根目录* 2).方法处: 提供进一步的细分映射信息。相对于类定义处的网址。若类定义处未标注@RequestMapping,则方法处标记的URL *相对于网应用的根目录*/@请求映射(/testRequestMapping )公共字符串testRequestMapping(){ system。出去。println( testRequestMapping );返回"成功";}}

 

  

2 value

@请求映射(/spring MVC )可以改为@请求映射(值= spring MVC )

 

  

3 method

职位请求

 

  控制器

  /***方法比较常用,用来指定请求方式* @ return */@请求映射(值=测试方法,方法=请求方法.帖子)公共字符串测试方法(){ system。出去。println(“测试方法”);返回"成功";}jsp

  表单操作=/springmvc/testMethod 方法=post 输入类型=提交值=提交/表单

  

4 params和headers

/** * 可以使用参数和头球来更加精确的映射请求。参数和头球支持简单的表达式. return */@ request mapping(value= testPramsAndHeaders ,params={username , age!=10},头={})公共字符串testPramsAndHeaders(){ system。出去。println( testPramsAndHeaders );返回"成功";} a href=/spring MVC/testPramsAndHeaders?username=excite rage=10 rel=外部无关注 testPramsAndHeaders/a当年龄=10岁时,访问会出错;当年龄为其他值时,正常访问。

 

  http://localhost :8080/spring MVC/testPramsAndHeaders?用户名=激励范围=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"; }

 

  

总结

到此这篇关于SpringMVC中@RequestMapping注解用法的文章就介绍到这了,更多相关SpringMVC@RequestMapping用法内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: