1首先@RequestMapping 中的值,咱们说请求方法l路径,请求url咱们都知道怎么请求了,在第一节helloworld中,web
咱们先说咱们先建一个类,RequestMappingTest spring
方法以下:浏览器
/**
* 1. @RequestMapping 除了修饰方法, 还可来修饰类 2. 1). 类定义处: 提供初步的请求映射信息。相对于 WEB 应用的根目录
* 2). 方法处: 提供进一步的细分映射信息。 相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL
* 相对于 WEB 应用的根目录
*/mvc
@RequestMapping("/requestMapping")
public String requestMapping(){
System.out.println("requestMapping()....");
return SUCCESS;
} app
方法名字和@RequestMapping中的值同样,这里面能够有两种方式:jsp
1当咱们在类中RequestMappingTest 上能够加注解也能够不加注解@RequestMappingpost
当咱们加注解的时候好比 @RequestMapping("/springMvc")
学习
那么咱们超连接的请求方式为 <a href="springMvc/requestMapping">springMvc/requestMapping</a><br/>测试
若是不加注解 <a href="requestMapping">requestMapping</a><br/> ui
2.咱们可使用 method 属性来指定请求方式
@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("testMethod");
return SUCCESS;
}
那么咱们超连接的请求方式为
<!--由于被注解是Post请求,自己请求是get,因此请求不到 -->
<a href="springMvc/testMethod">springMvc/testMethod</a><br/>
这样是错误的,正确的方法是要post请求,那么咱们能够建一个表单
<form action="springMvc/testMethod" method="post">
<input type="submit">
3.可使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式
/**
* 可使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式.
*
* @return
*/
@RequestMapping(value = "testParamsAndHeaders", params = { "username",
"age!=10" }, headers = { "Accept-Language=en-US,zh;q=0.8" })
public String testParamsAndHeaders() {
System.out.println("testParamsAndHeaders");
return SUCCESS;
}
那么咱们超连接的请求方式为
<a href="springmvc/testParamsAndHeaders?username=atguigu&age=10">Test ParamsAndHeaders</a>
<br><br> ,headers中的Accept-Language=en-US,zh;q=0.8,咱们能够用火狐浏览器按F12能够看到,这个是http协议中
的请求头中的内容,咱们也能够指定其余的,咱们看下这个超连接确定是错误的,由于咱们方法中设置的是age!=10,而咱们是age=10
因此,改过了就能够咯
4@PathVariable 能够来映射 URL 中的占位符到目标方法的参数中.
/**
* @PathVariable 能够来映射 URL 中的占位符到目标方法的参数中.
* @param id
* @return
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("testPathVariable: " + id);
return SUCCESS;
}
咱们超连接的请求方式为 :
<a href="springMvc/testPathVariable/101">springMvc/testPathVariable/101</a><br/>
5@RequestMapping 还 支持 Ant 格 URL
@RequestMapping("/testAntPath/*/abc")
public String testAntPath() {
System.out.println("testAntPath");
return SUCCESS;
}
咱们超连接的请求方式为 :
<a href="springMvc/testAntPath/ddd/abc">springMvc/testAntPath</a><br/>,ddd的值能够是任意的
6.咱们来讲下rest
咱们能够先建一个jsp代码以下:
<form action="springMvc/testRestPut/1" method="Post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="testRestPut">
</form><br>
<form action="springMvc/testRestDelete/1" method="Post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="testRestDelete">
</form><br>
<form action="springMvc/testRestPost/1" method="Post">
<input type="submit" value="testRestPost">
</form><br>
<a href="springMvc/testRestGet/1">testRestGet</a><br/>
咱们用方法来测试一下:
@RequestMapping(value="/testRestPut/{id}",method=RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id){
System.out.println("testRestPut()..."+id);
return SUCCESS;
}
@RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id){
System.out.println("testRestDelete()..."+id);
return SUCCESS;
}
@RequestMapping(value="/testRestPost/{id}",method=RequestMethod.POST)
public String testRestPost(@PathVariable Integer id){
System.out.println("testRestPost()..."+id);
return SUCCESS;
}
@RequestMapping("/testRestGet/{id}")
public String testRestGet(@PathVariable Integer id){
System.out.println("testRestGet()..."+id);
return SUCCESS;
}
一一跟上面对应,咱们来讲明一下,咱们知道原本就只有get,post请求,get,post请求咱们就不说了
如何将post请求转换成DELETE,和PUT呢,咱们要建一个表单,这样才能是post请求,
若是要转换,那么咱们要在表单中加入隐藏域,name属性必须为 _method,value的值能够是
DELETE,PUT,为何是这样呢,咱们要有个关键的细节没作,作了在说;咱们要在web.xml中加入
<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>
,咱们能够看下HiddenHttpMethodFilter 的源码,Alt +Shirt+T 进入后大家看到有个常量是_method,
因此隐藏域中的name属性必须是name属性必须为 _method,value是post要转换成的值,源码有兴趣的朋友
能够看下,value大小写都不要紧,源码中会所有转换成大写,
这样咱们用@PathVariable注解就能够传入参数,这样咱们就能够进行CRUD了,之前咱们学习
webservlet的时候 通常都是delete?id=1 这样,如今不用这样,这样很方便,今天咱们就讲到这里。
但愿看我博客的朋友继续关注哦,我会把全部学习到的知识所有更在博客上。