Spring MVC session Attribute set intially
I have used the Spring MVC. I set the Session Attribute value like
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String initHome(Model model) {
if (!model.containsAttribute("clientObject")) {
model.addAttribute("clientObject", createDefaultClient());
}
return "homeMenu";
}
It is working fine if i click the home menu url(/home). but if i did not
go the home means it says error as 'session attribute clientObject is
required'
so i decided to set sessionattibutes in constructor of controller
@Autowired
public MyController(Model model) {
if (!model.containsAttribute("clientObject")) {
model.addAttribute("clientObject", createDefaultClient());
}
}
it also says error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'myController'
I tried to set using the RequestMapping also like
@RequestMapping(value = "/", method = RequestMethod.GET)
public void initController(Model model) {
if (!model.containsAttribute("clientObject")) {
model.addAttribute("clientObject",
createDefaultClient());
}
}
this method is also not called intially my cointroller look like
@RequestMapping("/sample")
public class MyController {
..
..
is it possible to set the sessionAttribute value in the constructor of
controller? or any other way to set the session Attribute initially?
Thanks in advance for your help.
No comments:
Post a Comment