I have created two object in session scope like this @SessionAttributes({"userObj","simpleValue"})
in my controller.
I am adding a user object and String at these variables in my controller like this:
modelAndView.addObject("userObj", user);
modelAndView.addObject("simpleValue", "Hello World");
User
class is a simple class with 2 properties id
& name
.
Lets say I created this in a controller called Controller1
which shows Page1
. I can see the data of my session variables in page1
.
Now I created another controller say Controller2
(this controller has no relation with page1
or Controller1
) which displays page2
, now in this new page I am able to access only the single session attribute for simpleValue
, I am not able to access userObj
, I am getting empty result.
As per the @SessionAttributes, it says:
NOTE: Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation.
So I have 2 questions here:
1) I am not able to understand why Spring allowing me to access the simple property but not the User object in page2
.
2) The document also says we have to use either traditional session
or WebRequest
. I able to use the sessison
and access the variables, but can someone please help me how to use WebRequest
for storing objects in session?
This is the code that I am 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 & page2.jsp, both have same code.
<p>Session variable : ${simpleValue}</p>
<p>Product's 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
}
This is my configuration files:
Spring based configuration
@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;
}
}
Web based 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);
}
}