您的位置:首页 > 编程语言 > Java开发

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?

session.setAttribute("name","value");


and how to add element, that annotate by
@ModelAttribute
annotation 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
HttpSession
as 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:

@session
may 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}
.

SessionAttribute
annotation 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
@SessionAttributes
annotation. In below eg,
personObj
will 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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: