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; }
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); } }