首页 > 其他 > 详细

请求处理方法签名

时间:2018-02-06 00:01:51      阅读:272      评论:0      收藏:0      [点我收藏+]

一:请求处理方法签名介绍

1.介绍

  技术分享图片

 

二:@RequestParam

1.使用@RequestParam绑定请求参数

  注意点:required。

  技术分享图片

 

2.index.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <a href="helloworld3?username=cj&age=13">Test Rest Get</a>
11     <br>
12 </body>
13 </html>

 

3.RequestParamController.java

 1 package com.spring.it;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 
 7 @Controller
 8 public class RequestParamControl {
 9     @RequestMapping("/helloworld3")
10     public String hello(@RequestParam(value="username") String username,
11             @RequestParam(value="age") int age) 
12     {
13         System.out.println("username="+username+" ,age="+age);
14         return "success";
15     }
16 }

 

4.效果

  技术分享图片

 

5.衍生

  如果required该参数是否必须,如果是false时,下面还可能会用到参数,这个时候可以使用下面的方式处理。

  在里面可以添加一个默认属性的参数。

  @RequestParam(value=“age”,required=“false”,defaultValue=“0”)

 

三:@RequestHeader

1.@RequestHeader绑定请求报头

  技术分享图片

 

2.寻找请求头

  技术分享图片

 

3.以Accept-Language为例

  

4.index.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     测试请求头
11     <br>
12     <a href="helloworld4/">Accept-Language</a>
13     <br><br>
14     
15     测试请求参数
16     <br>
17     <a href="helloworld3?username=cj&age=13">Test Rest Get</a>
18     <br>
19 </body>
20 </html>

 

5.java

 1 package com.spring.it;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestHeader;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 @Controller
 8 public class RequestParamControl {
 9     @RequestMapping("/helloworld4")
10     public String hello(@RequestHeader(value="Accept-Language") String al) 
11     {
12         System.out.println("Accept-Language="+al);
13         return "success";
14     }
15 }

 

6.效果

  技术分享图片

 

 

 

 

 

 

  

 

请求处理方法签名

原文:https://www.cnblogs.com/juncaoit/p/8419705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!