I have a session attribute : user, and I have a url that I want to be viewed by both logged in users and publically by people not logged in as a user.
So what I want to do is this :
@Controller("myController")
@SessionAttributes({"user"})
public class MyController {
@RequestMapping(value = "/MyPage/{id}", method = RequestMethod.GET)
public ModelAndView getPage(@PathVariable Integer id) {
return modelandview1;
}
@RequestMapping(value = "/MyPage/{id}", method = RequestMethod.GET)
public ModelAndView getPage(@PathVariable Integer id, @ModelAttribute User user){
return modelandview2;
}
However, I have a feeling its not going to work … suggestions very welcome.
I don’t think it’s a right case for
@SessionAttributes. This annotation is usually used to keep original instance of a form-backing object, to avoid passing irrelevant parts of its state via hidden form fields.Your sceanrio is completely different, thus it would be better to use
HttpSessionexplicitly:Also note that
@ModelAttributeis a subject to data binding – user can change its fields by passing request parameters. You definitely don’t want it in this case.