Python 官方文档:入门教程 => 点击学习
目录1 修饰类和方法2 value3 method4 params和headers5 Ant路径5 @PathVariable6 HiddenHttpMethodFilter7 @R
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";
}
}
@RequestMapping("/springmvc")
可以改为@RequestMapping(value = "springmvc")
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>
@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
@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>
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" %>
@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>
@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>
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value = "JSESSIONID") String sessionId) {
System.out.println("testCookieValue:" + sessionId);
return "success";
}
到此这篇关于SpringMVC中@RequestMapping注解用法的文章就介绍到这了,更多相关SpringMVC @RequestMapping用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringMVC中@RequestMapping注解用法实例
本文链接: https://lsjlt.com/news/152359.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0