006 请求处理方法签名

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

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