@CookieValue注解获取Cookie信息
该注解用来获取Cookie中指定Key的值,在ParamController中增加getCookies方法
@RequestMapping("/cookies")
public String getCookies(@CookieValue(value = "JSESSIONID",required = false, defaultValue = "默认值") String jsessionId){
System.out.println("Cookies中的Key为JSESSIONID的值为:" + jsessionId);
return "success";
}
复制代码
浏览器中输入http://localhost:8080/cookies, 控制台打印出Cookie中的key的Value

Servlet API
Spring MVC 同样支持Servlet API,往request和session中保存数据,
@RequestMapping("/servlet")
public String servletApi(HttpServletRequest request, HttpServletResponse response, HttpSession session){
request.setAttribute("reqAttr","request中保存的数据");
session.setAttribute("sessAttr","session中保存到数据");
return "success";
}
复制代码
取出session和request中保存的值,在success.jsp的body标签中中增加代码
<h3>请求中保存的数据</h3>
<p>${requestScope.reqAttr}</p>
<h3>session中保存的数据</h3>
<p>${sessionScope.sessAttr}</p>
复制代码
重启Tomcat,浏览器访问http://localhost:8080/servlet

Spring MVC 可以接收的 Servlet API参数,出了HttpServletRequest、HttpServletResponse、HttpSession外还有
- java.security.Principal
- Locale
- InputStream
- OutputStream
- Reader
- Writer
对象入参
增加一辆Tesla为例,前端增加输入Tesla信息的form表单,在index.jsp中改造增加Tesla表单的内容,增加Tesla信息输入框
<h3>增加Tesla车型</h3>
<form action="/tesla" method="post">
车型: <input type="text" name="name"> <br>
价格: <input type="text" name="price"> <br>
<hr>
<button type="submit">增加Semi Truck</button>
</form>
复制代码
新增entity包,新增一个Tesla对象,属性与表单中的填写的信息一致
public class Tesla {
private String name;
private Double price;
//此处省略getter/setter/toString信息
}
复制代码
修改TeslaController中的add方法
@RequestMapping(value = "/tesla", method = RequestMethod.POST)
public String add(Tesla tesla){
System.out.println("增加Tesla车型为:" + tesla);
return "success";
}
复制代码
重启tomcat,在表单中输入信息,并提交,控制台打印出提交的信息

说明请求中的属性会自动封装到POJO对象
给Tesla实体类增加一个owner属性,表示车主,并增加Owner实体类
public class Owner {
private String username;
private Integer age;
//省略getter/setter/toString方法
}
复制代码
表单增加owner信息输入框
<h3>增加Tesla车型</h3>
<form action="/tesla" method="post">
车型: <input type="text" name="name"> <br>
价格: <input type="text" name="price"> <br>
车主姓名: <input type="text" name="owner.username"> <br>
车主年龄: <input type="text" name="owner.age">
<hr>
<button type="submit">增加Semi Truck</button>
</form>
复制代码
重启Tomat,控制台输出Tesla级联属性Owner的信息

表单中输入中文,会出现乱码

中文乱码问题解决方法归类
请求乱码
- GET请求乱码,修改Tomcat中的server.xml配置文件,在8080端口出增加URIEncoding="UTF-8"
- POST请求乱码,在获取请求参数前增加request.setCharacterEncoding("UTF-8") 响应乱码
- 代码最后增加response.setContenttype("text/html;charset=utf-8")
也可以在web.xml中配置过滤器解决乱码问题,解决乱码问题的filter一定要放在所有filter前,字符编码filter放在其他所有filter前面配置
<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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
复制代码
重启Tomcat,再次提交请求

三、Spring MVC 数据输出
如何将数据带到页面上?
Spring MVC出了可以通过request和session将数据带到页面上,还可以在方法处传入Map、Model、ModelMap,在这些参数中保存数据都会被放在请求域中,可以在页面获取
Map
新增一个OutputDataController,增加方法outputByMap方法
@Controller
public class OutputDataController {
@RequestMapping("/map")
public String outputByMap(Map<String, Object> map){
map.put("mapKey","map中保存的数据");
return "success";
}
}
复制代码
页面获取数据
<h3>Map中保存的数据</h3>
<p>从Request域中获取:${requestScope.msg}</p>
<p>从Session域中获取:${sessionScope.msg}</p>
<p>从PageContext域中获取:${pageScope.msg}</p>
<p>从Application域中获取:${applicationScope.msg}</p>
复制代码
重启Tomcat,浏览器输入http://localhost:8080/map

只有从请求域中可以获取到数据
Model
新增方法outputByModel
@RequestMapping("/model")
public String outputByModel(Model model){
model.addAttribute("msg", "I am IRONMAN");
return "success";
}
复制代码
重启tomcat,浏览器访问

ModelMap
ModelMap是Model接口的实现类,也可以用作数据输出
@RequestMapping("/model_map")
public String outputByModelMap(ModelMap modelMap){
modelMap.addAttribute("msg", "I am IRONMAN");
return "success";
}
复制代码
重启tomcat,浏览器访问

在三个方法中打印map、model、modelMap的class,重启后在浏览器请求三个方法上标注的URL地址

Map、Model、ModelMap最终都是BindingAwareModelMap在实际发挥作用
Spring MVC还可以使用其他方式输出数据
- ModelAndView:处理方法值返回类型为ModelAndView,方法可以通过该对象添加数据及返回的页面
- @SessionAttributes:将数据存储到Session中,多个请求之间可以共享数据,不推荐使用
- @ModelAttribute:方法如惨标注后,入参的对象就可以方法数据模型中,不常用
ModelAndView
页面和数据的合体对象,创建ModelAndView对象时传入的参数就是一个页面,也就是要返回的页面
@RequestMapping("model_and_view")
public ModelAndView outputByModelAndView(){
ModelAndView modelAndView = new ModelAndView("success");
modelAndView.addObject("msg","I am IRONMAN");
return modelAndView;
}
复制代码
重启tomcat,浏览器输入http://localhost:8080/model_and_view

@SessionAttributes
新建一个类,增加@SessionAttributes注解,@SessionAttributes注解只能放在类上
@SessionAttributes(value = "msg")
@Controller
public class SessionController {
@RequestMapping("/session_attributes")
public ModelAndView outputBySession(){
ModelAndView modelAndView = new ModelAndView("success");
modelAndView.addObject("msg","I am God of Thunder");
return modelAndView;
}
@RequestMapping("/user")
public ModelAndView getUser(){
ModelAndView modelAndView = new ModelAndView("success");
return modelAndView;
}
}
复制代码
先访问http://localhost:8080/session_attributes

在访问http://localhost:8080/user

都可以获取到session中保存的数据
@SessionAttributes有两个属性 value:只要保存的Key是value指定的,就将它保存在Session中 types:只要保存的是指定类型的资源,就将它保存在Session中
测试types属性
@SessionAttributes(types = Tesla.class)
@Controller
public class SessionController {
@RequestMapping("/session_attributes")
public ModelAndView outputBySession(){
ModelAndView modelAndView = new ModelAndView("success");
modelAndView.addObject("msg","I am God of Thunder");
return modelAndView;
}
@RequestMapping("/user")
public ModelAndView getUser(){
ModelAndView modelAndView = new ModelAndView("success");
return modelAndView;
}
@RequestMapping("/session/tesla")
public ModelAndView getTesla(){
ModelAndView modelAndView = new ModelAndView("success");
Tesla tesla = new Tesla();
tesla.setName("Model 3");
modelAndView.addObject("msg", tesla);
return modelAndView;
}
}
复制代码
重启Tomcat,分被在浏览器中输入三个地址



可以看出在Session中存储的是types指定的Tesla数据类型
@SessionAttribute不推荐使用,推荐使用原生API