How to store data is a session area in Spring

I created two objects in the session scope, such as @SessionAttributes({"userObj","simpleValue"}) in my controller.

I add a custom object and a String to these variables in my controller as follows:

 modelAndView.addObject("userObj", user); modelAndView.addObject("simpleValue", "Hello World"); 

User class is a simple class with 2 id and name properties.

Suppose I created this in a controller called Controller1 , which shows Page1 . I see the data of my session variables in Page1 .

Now I created another controller: Controller2 (this controller is not related to Page1 or Controller1 ) that displays page2 , now on this new page I can access only one session attribute for simpleValue , I can not access userObj , I I get an empty result.

According to @SessionAttributes , it says:

NOTE. Session attributes specified in this annotation correspond to specific attributes of the processor model, transparent storage in a conversation. These attributes will be deleted after the handler indicates the end of the conversation. Therefore, use this tool for dialog attributes that you intend to temporarily store in a session for a special handler session.

So, I have 2 questions:

1) I cannot understand why Spring allows me to access a simple property, but not a User object in page2 .

2) The document also states that we should use either traditional session or WebRequest . I can use sessison and access variables, but can anyone help me how to use WebRequest to store objects in a session?

This is the code I'm using:

Controller1.java

 @Controller @SessionAttributes({"mySessionAttr","userObj"}) public class Controller1 { @RequestMapping(value="/page1") public ModelAndView singleFieldPage() { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("mySessionAttr", "Hello World"); modelAndView.addObject("userObj", new User(1,"Scott")); modelAndView.setViewName("page1"); return modelAndView; } } 

Controller2.java

 @Controller public class Controller2 { @RequestMapping(value="/page2") public ModelAndView singleFieldPage(HttpSession session) { return new ModelAndView("page2"); } } 

page1.jsp and page2.jsp , both have the same code.

 <p>Session variable : ${simpleValue}</p> <p>Product name is ${userObj.name}. The id is ${userObj.id} 

This is my User.java :

public class User {

 private int id; private String name; public User() { } public User(int id, String name) { this.id = id; this.name = name; } // Setters & Getters } 

These are my configuration files:

Spring

 @Configuration @ComponentScan("com.examples") @EnableWebMvc public class WebAppConfig { @Bean public UrlBasedViewResolver setupViewResolver() { UrlBasedViewResolver resolver = new UrlBasedViewResolver(); resolver.setPrefix("/WEB-INF/pages/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); return resolver; } } 

Website configuration

 public class Initializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebAppConfig.class); ctx.setServletContext(servletContext); Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } } 
+1
source share
2 answers

From my point of view, the problem with the user object is that the User class does not implement Serializable .

And as for WebRequest , this is basically an abstraction for NativeWebRequest and AsyncWebRequest , as you can see in the documentation

Common interface for web request. Mainly intended for the general network to request interceptors, giving them access to the metadata of the general request, and not for the actual handling of the request.

With this approach, WebRequestInterceptor can be used for servlets, as well as for requests in portlets as state documentation :

Interface for the general interception of web requests. Allows you to apply to a Servlet request, as well as to Portlet request environments based on the WebRequest abstraction.

+1
source

@SessionAttribute is bound to a single controller, and thus the controller is also responsible for managing this session context. If you need conversations, use the web stream.

Originally posted by javadoc

The session attributes specified with this annotation correspond to specific attributes of the handler model that are transparently stored in the conversation session. These attributes will be deleted after the handler indicates the end of the session. Therefore, use this tool for dialog attributes that should be temporarily stored in a session during a particular handler session.

Although the term "conversation" is used here, it is mainly a conversation with one handler (in this case, a controller), and not with several handlers. For such cases and better control, use Spring Web Flow.

0
source

All Articles