将HttpServletRequest
作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象
<a th:href="@{/testServletAPI(username='张三',password='123456')}">测试a>
@Controller
public class paramController {
@RequestMapping("/testServletAPI")
public String testParam(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username:"+username+",password:"+password);
return "param_test";
}
}
在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映射时,在DispatcherServlet
中就会将请求参数赋值给相应的形参
<a th:href="@{/testServletAPI(username='张三',password='123456')}">测试a>
@Controller
public class paramController {
@RequestMapping("/testServletAPI")
public String testParam(String username,String password){
System.out.println("username:"+username+",password:"+password);
return "param_test";
}
}
<form th:action="@{/testParam}" method="post">
姓名:<input type="text" name="username">
密码:<input type="password" name="password">
爱好:<input type="checkbox" name="hobby" value="a">a
<input type="checkbox" name="hobby" value="b">b
<input type="checkbox" name="hobby" value="c">c<br>
<button type="submit" >提交button>
form>
@Controller
public class paramController {
@RequestMapping("/testParam")
public String testParam(String username,String password,String hobby){
System.out.println("username:"+username+",password:"+password+",hobby:"+hobby);
return "param_test";
}
}
@Controller
public class paramController {
@RequestMapping("/testParam")
public String testParam(String username,String password,String[] hobby){
System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));
return "param_test";
}
}
注: 若请求所传输的请求参数中有多个同名的请求参数,此时可以在控制器方法的形参中设置字符串 数组或者字符串类型的形参接收此请求参数
若使用字符串数组类型的形参,此参数的数组中包含了每一个数据 若使用字符串类型的形参,此参数的值为每个数据中间使用逗号拼接的结果
@RequestParam(“user_name”) String username。添加一个注解
<form th:action="@{/testParam}" method="get">
姓名:<input type="text" name="user_name">
密码:<input type="password" name="password">
爱好:<input type="checkbox" name="hobby" value="a">a
<input type="checkbox" name="hobby" value="b">b
<input type="checkbox" name="hobby" value="c">c<br>
<button type="submit" >提交button>
form>
@Controller
public class paramController {
@RequestMapping("/testParam")
public String testParam(@RequestParam("user_name") String username, String password, String[] hobby){
System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));
return "param_test";
}
}
设置这个参数非必须
@RequestMapping("/testParam")
public String testParam(@RequestParam(value = "user_name",required = false) String username, String password, String[] hobby){
System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));
return "param_test";
}
默认是必须传值
@RequestMapping("/testParam")
public String testParam(@RequestParam("user_name") String username, String password, String[] hobby){
System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));
return "param_test";
}
@Controller
public class paramController {
@RequestMapping("/testParam")
public String testParam(@RequestParam(value = "user_name", required = false,defaultValue = "hello") String username, String password, String[] hobby) {
System.out.println("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby));
return "param_test";
}
}
@Controller
public class paramController {
@RequestMapping("/testParam")
public String testParam(@RequestParam(value = "user_name", required = false,defaultValue = "hello") String username, String password, String[] hobby,
@RequestHeader(value = "ShowHost" ,required = true,defaultValue = "MyHost") String host) {
System.out.println("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby)+",Host:"+host);
return "param_test";
}
}
可以在控制器方法的形参位置设置一个实体类类型的形参,此时若浏览器传输的请求参数的参数名和实体类中的属性名一致,那么请求参数就会为此属性赋值
<form th:action="@{/myTestParam}" method="get">
姓名:<input type="text" name="username">
密码:<input type="password" name="password">
性别:<input type="text" name="sex">
<button type="submit" >提交button>
form>
@Controller
public class paramController {
@RequestMapping("/myTestParam")
public String myTest(User user){
System.out.println(user);
return "param_test";
}
}
解决获取请求参数的乱码问题,可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter,但是必须在web.xml中进行注册
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
学无止境。。。。。。