标签:required span put 方案 frame 自动 ali string action
处理模型数据
注:下面的代码均来自上一篇的注解代码之后的测试代码!!
ModelAndView
TestRequestMapping.java
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
String viewName="success";
ModelAndView andView = new ModelAndView(viewName);
andView.addObject("time", new Date());
return andView;
}
index.jsp
<a href="springmvc/testModelAndView">testModelAndView</a>
success.jsp;
time:${requestScope.time}
总结:SpringMVC会把ModelAndView的model中数据放入到request域对象中
Map 及 Model
Spring MVC 在内部使用了一个org.springframework.ui.Model 接口存储模型数据
目标方法可以添加Map类型(实际上是Model或者ModelMap类型)的参数
TestRequestMapping.java
@RequestMapping("/testMap") public String testMap(Map<String, Object> map){ map.put("name", "MrChengs"); return "success"; }
indexjsp
<a href="springmvc/testMap">testMap</a>
success.jsp
name:${requestScope.name}
@SessionAttributes
只能放在类的上面
TestRequestMapping.java
@SessionAttributes(value="user") @Controller @RequestMapping("/springmvc") public class TestRequestMapping { @RequestMapping("/testSessionAttributes") public String testSessionAttributes(Map<String,Object> map){ User user = new User("MrChangs", "1234", "1287@qq.com"); map.put("user", user); return "success"; } }
index.jsp
<a href="springmvc/testSessionAttributes">testSessionAttributes</a>
success.jsp
user requestScope:${requestScope.user} <br> <br> user sessionScope:${sessionScope.user}
@ModelAttribute
<!--
模拟修改操作
1.原始数据:id=1,name=MrChengs,pw=1234,email=MrChengs@qq.com
2.密码不能修改
3.表单回显,模拟操作直接在表单填写对应的额属性值
-->
<form action="springmvc/ModelAttribute" method="post">
<input type="hidden" name="id" value="1">
<br>
name:<input type="text" name="username" value="MrChengs">
<br>
email:<input type="text" name="email" value="MrChengs@qq.com">
<br>
<input type="submit" value="submit">
</form>
//标记的方法会在每个目标方法执行之前被调用 //1.由@ModelAttribute标记的方法,会把每个目标之前被springmvc调用 //2.@MOdelAttribute注解也可以来修饰目标方法pojo类型的入参,其value属性值如下作用 //2.1)springmvc会使用value属性值在implicitModel中查找对应的对象,若存在直接传入到目标方法的入参中 //2.2)springmvc会把value为key,pojo类型对象为value,存到request中 @ModelAttribute public void getUser(@RequestParam(value="id",required=false) Integer id, Map<String,Object> map){ if(id != null){ User user = new User(1, "MrChengs", "1234", "MrChengs@qq.com"); System.out.println("得到一个参数"); //注意:这里的key为users,如果改为users等其他字符,等不到结果,程序可以正常的执行 //解决方法在下面的代码中 map.put("users", user); } } @RequestMapping("/ModelAttribute") public String testModelAttribute(@ModelAttribute("users")User user){ System.out.println("update:" + user); return "success"; }
user:${requestScope.users}
如果不添加@ModelAttribut,在测试中代码的值为null
@SessionAttributes(value="user") @Controller @RequestMapping("/springmvc") public class TestRequestMapping { @RequestMapping("/ModelAttribute") public String testModelAttribute(User user){ System.out.println("update:" + user); return "success"; } }
public String testModelAttribute(@ModelAttribute("acbc")User user){
十月 16, 2018 4:38:06 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/Spring_MVC_01] threw exception [Session attribute ‘user‘ required - not found in session] with root cause org.springframework.web.HttpSessionRequiredException: Session attribute ‘user‘ required - not found in session at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:791)
@ModelAttribute
由@SessionAttributes引发的异常
org.springframework.web.HttpSessionRequiredExcept: Session attribute ‘user‘ required - not found in session
如何避免@SessionAttributes引发的异常
标签:required span put 方案 frame 自动 ali string action
原文地址:https://www.cnblogs.com/Mrchengs/p/9940917.html