(一)servletAPI获取请求参数
通过request.getParameter获取
<form th:action="@{/testServlet}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="测试servlet获取请求参数">
</form>
//通过ServletAPI获取请求参数
@RequestMapping("/testServlet")
public String test(HttpServletRequest request){
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username);
System.out.println(password);
return "success";
}
(二)通过控制器方法的形参来获取请求参数
2.1 直接获取
<form th:action="@{/testSpringmvc}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
爱好:<input type="checkbox" name="hobby" value="a">a
爱好:<input type="checkbox" name="hobby" value="b">b
爱好:<input type="checkbox" name="hobby" value="c">c
<input type="submit" value="测试servlet获取请求参数">
</form>
直接在形参上填写需要获取的参数,就可以获取
//通过控制器方法获取请求参数
@RequestMapping("/testSpringmvc")
public String test(String username,String password,String [] hobby){
System.out.println(username);
System.out.println(password);
System.out.println(hobby[0]);
return "success";
}
2.2 @RequestParam注解
1、@RequestParam是将请求参数和控制器方法的形参创建映射关系
2、@RequestParam注解一共有三个属性:
- value:指定为形参赋值的请求参数的参数名
- required:设置是否必须传输此请求参数,默认值为true,若设置为true时,则当前请求必须传输value所指定的请求参数,若没有传输该请求参数,且没有defaultValue属性,则页面报错400:Required String parameter ‘xxx’ is not present;若设置为false,则当前请求不是必须传输value所指定的请求参数,若没有传输,则注解所标识的形参的值为null
- defaultValue:不管required属性值为true或false,当value所指定的请求参数没有传输或传输的值为""时,则使用默认值为形参赋值
<form th:action="@{/testRequestParam}" method="post">
用户名:<input type="text" name="user_name"><br>
密码:<input type="password" name="password"><br>
爱好:<input type="checkbox" name="hobby" value="a">a
爱好:<input type="checkbox" name="hobby" value="b">b
爱好:<input type="checkbox" name="hobby" value="c">c
<input type="submit" value="测试servlet获取请求参数">
</form>
@RequestMapping("/testRequestParam")
public String test(
//这里user_name要与前端页面中的name相同
@org.springframework.web.bind.annotation.RequestParam("user_name") String username,
String password
){
System.out.println(username);
return "success";
}
2.3通过对象获取请求参数
可以在控制器方法的形参位置设置一个实体类类型的形参,此时若浏览器传输的请求参数的参数名和实体类中的属性名一致,那么请求参数就会为此属性赋值。
创建user实体类
<form th:action="@{/testpojo}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
性别:<input type="radio" name="sex" value="男">男<input type="radio"
name="sex" value="女">女<br>
年龄:<input type="text" name="age"><br>
邮箱:<input type="text" name="email"><br>
<input type="submit">
</form>
获取到user对象 打印其中的内容
@RequestMapping("/testpojo")
public String testPOJO(User user){
System.out.println(user);
return "success";
}
//最终结果-->User{id=null, username='张三', password='123', age=23, sex='男',
email='123@qq.com'}
(三)解决获取请求参数乱码问题
在JavaWeb阶段,解决请求参数乱码问题分为两种
1、解决get请求乱码问题:这是由于Tomcat服务器产生的,所以解决方法是在Tomcat中的web.xml文件中,直接设置请求参数的编码方式为utf-8
2、解决Post请求乱码问题:是在获取请求参数之前调用request.setCharacterEncoding(“UTF-8”);方法来进行解决。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 解决post请求中文乱码问题
// 一定要在获取请求参数之前调用才有效
request.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
try {
// 获取 action 业务鉴别字符串,获取相应的业务 方法反射对象
Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class,
HttpServletResponse.class);
System.out.println(method);
// 调用目标业务 方法
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
在SpringMVC中,请求参数是由前端控制器所获取的,所以在controller方法中去设置编码方式是无效的(需要在获取请求参数之前调用),所以springMVC中解决post请求参数乱码使用SpringMVC提供的编码过滤器CharacterEncodingFilter。需要在web.xml文件中进行注册
<!--配置springMVC的编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-
class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:SpringMVC中处理编码的过滤器一定要配置到其他过滤器之前,否则无效
(四)ServletAPI向request域对象共享数据
采用setAttribute向testScope中共享数据
<a th:href="@{/servletApi}">测试servletApi</a>
@RequestMapping("/servletApi")
public String servletApi(HttpServletRequest servletRequest){
servletRequest.setAttribute("testScope","hello servlet");
return "success";
}
(五)使用SpringMVC向request域对象共享数据
5.1 使用ModelAndView向向request域对象共享数据
Model中存的是域对象共享的数据,View是返回的视图名称
<a th:href="@{/TestModelAndView}">测试TestModelAndView</a><br>
@RequestMapping("/TestModelAndView")
public ModelAndView modelandview(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("testScope","hello ModelandView");
modelAndView.setViewName("success");
return modelAndView;
}
5.2 使用Model向request域对象共享数据
@RequestMapping("/TestModel")
public String model(Model model){
model.addAttribute("testScope","hello,model");
return "success";
}
5.3 使用map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
5.4 使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
5.5 Model、ModelMap、Map的关系
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}