How to use Session attributes in Spring-mvc
2015-09-09 07:14
651 查看
Could you help me write spring mvc style analog so code in jsp and servlets?
and how to add element, that annotate by
If you want to deleting object after each response no need to session,
If you want keep object during user session , It has some ways:
directly add one attribute to session: declare
and you can get it from controller like this :
Make your controller session scoped
Scope the Objects ,for example you have user object that want be in session every time:
then inject class in each controller that you want
that keep class on session.
The AOP proxy injection : in spring -xml:
then inject class in each controller that you want
each one has advantage and disadvantage:
The below annotated code would set “value” to “name”
To access the same in JSP use
session.setAttribute("name","value");
and how to add element, that annotate by
@ModelAttributeannotation to session and after get acces to it?
If you want to deleting object after each response no need to session,
If you want keep object during user session , It has some ways:
directly add one attribute to session: declare
HttpSessionas a parameter to your controller method
@RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){ ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",value); return "testJsp"; }
and you can get it from controller like this :
ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
Make your controller session scoped
@Controller @Scope("session")
Scope the Objects ,for example you have user object that want be in session every time:
@Component @Scope("session") public class User { String user; /* setter getter*/ }
then inject class in each controller that you want
@Autowired private User user
that keep class on session.
The AOP proxy injection : in spring -xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <bean id="user" class="com.User" scope="session"> <aop:scoped-proxy/> </bean> </beans>
then inject class in each controller that you want
@Autowired private User user
each one has advantage and disadvantage:
@sessionmay use more memory in cloud systems it copies session to all nodes, and direct method has messy approach, it is not good to unit test.
The below annotated code would set “value” to “name”
@RequestMapping("/testing") @Controller public class TestController { @RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){ request.getSession().setAttribute("name", "value"); return "testJsp"; } }
To access the same in JSP use
${sessionScope.name}.
SessionAttributeannotation is the simplest and straight forward instead of getting session from request object & setting attribute… Any object can be added to the model in controller and it will stored in session if its name matches with the argument in
@SessionAttributesannotation. In below eg,
personObjwill be available in session.
@Controller @SessionAttributes("personObj") public class PersonController { @RequestMapping(value="/person-form") public ModelAndView personPage() { return new ModelAndView("person-page", "person-entity", new Person()); } @RequestMapping(value="/process-person") public ModelAndView processPerson(@ModelAttribute Person person) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("person-result-page"); modelAndView.addObject("pers", person); modelAndView.addObject("personObj", person); return modelAndView; } }
相关文章推荐
- Get Session in Spring AOP
- Struts2中action获取request、response、session的方式
- 【小熊刷题】Binary Tree Right Side View <Leetcode 199, Java>
- Java 反射,通过反射调用所有的方法,包括private方法
- 《Java核心技术卷一》笔记 多线程
- 【Java】增强的for流程
- java笔记——面向对象设计模式
- 【学习日记】有关java的IO的知识点总结
- 用Java控制台应用程序做的万年历
- JAVA测试装饰者模式
- 爱心java
- java之 ------ 类反射【详解】
- springmvc处理静态资源
- Ubuntu12.04安装JDK1.7
- Json转换为java对象
- 大家都用什么Maven插件?及本人的几个常用插件分享
- java synchronized详解
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- java volatile内存机制
- Eclipse 点击ctrl键 open declaration;open implementat