好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于SpringMVC @RequestMapping的参数和用法

@requestmapping的参数和用法

requestmapping里面的注解包含的参数如图:

requestmapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

@requestmapping 除了修饰方法, 还可来修饰类 :

类定义处:提供初步的请求映射信息。相对于 web 应用的根目录;

方法处:提供进一步的细分映射信息。 相对于类定义处的 url。

若类定义处未标注 @requestmapping,则方法处标记的 url相对于 web 应用的根目录

返回modelandview时的url会根据你的 @requestmapping实际情况组成。

如果类上没有映射,那么url直接就是方法的映射;否则url为类上+方法上映射路径组合。

对应项目jsp位置则是一级路径对应一级文件目录。

如url为/default/index对应项目中webapp/default/index.jsp

requestmapping注解有六个属性,下面我们把她分成三类进行说明

【1】value, method

value :指定请求的实际地址,指定的地址可以是uri template 模式;

method : 指定请求的method类型, get、post、put、delete等;

【2】consumes,produces

consumes : 指定处理请求的提交内容类型(content-type),例如application/json, text/html;

produces :指定返回的内容类型,仅当request请求头中的(accept)类型中包含该指定类型才返回;

【3】params,headers

params : 指定request中必须包含某些参数值时,才让该方法处理。

headers : 指定request中必须包含某些指定的header值,才能让该方法处理请求。

测试示例如下:

【1】value||path jsp 页面

?

1

<a href= "springmvc/testrequestmapping" rel= "external nofollow" >test requestmapping</a>

controller

?

1

2

3

4

5

@requestmapping (value= "/testrequestmapping" )

public string testrequestmapping() {

     system.out.println( "testrequestmapping" );

     return success;

}

成功返回success.jsp 。

tips :若 href 属性值,不等于value值,则将提示404错误。

value的uri值为以下三类:

a) 可以指定为普通的具体值;

如下:

?

1

@requestmapping ( "/testrequestmapping" )

b) 可以指定为含有某变量的一类值(uri template patterns with path variables)–restful风格;

?

1

2

3

4

5

@requestmapping ( "/testpathvariable/{id}" )

public string testpathvariable( @pathvariable integer id2) {

     system.out.println( "testpathvariable: " + id2);

     return success;

}

除了value还有path,二者效果等同,可以参考源码如下图:

其中关于@pathvariable 有如下说明:

① 如果路径中的变量与方法中的变量名一致,可直接使用@pathvariable;

② 如果二者不一致,则使用@pathvariable(variable)显示指定要绑定的路径中的变量 。

@pathvariable只能绑定路径中的占位符参数,且路径中必须有参数。

@pathvariable用法参考  路径参数绑定参考

?

1

2

3

4

5

6

@requestmapping ( "/testpathvariable/{id}" )

public string testpathvariable( @pathvariable ( "id" ) integer id2) {

     system.out.println( "testpathvariable: " + id2);

     return success;

}

//路径中的 id 与 方法中的 id2 绑定

c) 可以指定为含正则表达式的一类值( uri template patterns with regular expressions);

?

1

2

3

4

5

   @requestmapping ( "/spring-web/{symbolicname:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}" ) 

   public void handle( @pathvariable string version, @pathvariable string extension) {     

     // ... 

   } 

}

【2】method jsp 页面

?

1

2

<a href= "springmvc/testmethod" rel= "external nofollow" >test method</a>

//href 默认为get 请求。

controller–限制接收post 请求。

?

1

2

3

4

5

@requestmapping (value = "/testmethod" , method = requestmethod.post)

public string testmethod() {

     system.out.println( "testmethod" );

     return success;

}

result as follows :

http status 405 - request method ‘get' not supported 。

【状态码405表示:请求中指定的方法不被允许。】

将method 改为method = requestmethod.get正常跳转页面。

【3】consumes jsp 页面

仍以testmethod为例,提交表单。

默认contenttype为content-type:application/x-www-form-urlencoded。

?

1

2

3

4

<form action= "springmvc/testmethod" method= "post" >

<input type= "text" name= "username" value= "" />

<input type= "submit" value= "submit" />

</form>

controller–限制接收post 请求以及consumes="application/json"。

?

1

2

3

4

5

@requestmapping (value = "/testmethod" , method = requestmethod.post,consumes= "application/json" )

public string testmethod() {

     system.out.println( "testmethod" );

     return success;

}

result as follows :

【状态码415表示:由于媒介类型不被支持,服务器不会接受请求。。】

去掉 consumes属性,页面正常跳转 !

【4】produces

后台代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

     @requestmapping (value = "/testmethod" , method = requestmethod.post,produces= "application/json" )

     public void testmethod2(httpservletrequest request,httpservletresponse response,model model)

     throws ioexception {

         request.getheader( "accept" );

         system.out.println(request.getheader( "accept" ));

//      response.setcontenttype("application/json");

         string username = request.getparameter( "username" );

         system.out.println( "testmethod..." +username);

         model.addattribute( "user" , username);

         object jsonstring =  "{'name': 'helloworlda'}" ;

         jsonobject jsonobj=jsonobject.fromobject(jsonstring);

         printwriter out = response.getwriter();

         out.print(jsonobj);

     }

浏览器请求头

?

1

text/html,application/xhtml+xml,application/xml;q= 0.9 ,image/webp,*/*;q= 0.8

其中最后一项 : */*;q=0.8。

该项表明可以接收任何类型的,权重系数0.8表明如果前面几种类型不能正常接收。则使用该项进行自动分析。

application/json 几种主流浏览器都可以自动解析。

【5】params jsp页面

?

1

2

3

4

5

form action= "springmvc/testparamsandheaders" method= "post" >

<input type= "text" name= "username" value= "" />

<input type= "text" name= "age" value= "" />

<input type= "submit" value= "submit" />

</form>

参数 username=tom;age = 10;

后台代码:

设定必须包含username 和age两个参数,且age参数不为10 (可以有多个参数)。

?

1

2

3

4

5

@requestmapping (value = "testparamsandheaders" , params = { "username" , "age!=10" })

     public string testparamsandheaders() {

         system.out.println( "testparamsandheaders" );

         return success;

     }

result as follows :

【状态码400表示:服务器未能理解请求。 】

将age 改为其他值,正常跳转。

【6】headers 浏览器请求头如下:

后台测试代码如下:

?

1

2

3

4

5

@requestmapping (value = "testparamsandheaders" , params = { "username" , "age!=10" }, headers = { "accept-language=us,zh;q=0.8" })

     public string testparamsandheaders() {

         system.out.println( "testparamsandheaders" );

         return success;

     }

设定请求头中第一语言必须为us。

result as follows :

【状态码404表示:服务器无法找到被请求的页面。】

将后台代码改为zh-cn。。。

页面正常跳转。

【tips】:

① 服务器首先根据url去找页面,如果找不到就返回404;

② 如果找到,但是不能正常处理,就会返回 5xx 类型错误。

其中在第一步过程中,会根据请求头进行一系列判断 !

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://janus.blog.csdn.net/article/details/55193269

查看更多关于基于SpringMVC @RequestMapping的参数和用法的详细内容...

  阅读:18次