Hi,
I have in my spring configuration file :
- a bean which is session-scoped
- an interceptor :Code:<!-- ************** --> <!-- context --> <!-- ************** --> <bean id="myBeanInSession" class="blabla.BeanInSession" scope="session"> </bean>
I have annotate-based controllers like this :Code:<!-- ************ --> <!-- interceptor --> <!-- ************ --> <bean name="securityInterceptor" class="web.interceptor.SecurityInterceptor"> </bean>
If in the securityInterceptor i put the code :Code:@Controller @SessionAttributes("myBeanInSession") public class GetContentController { @RequestMapping(value="/content.get", method=RequestMethod.GET) public String get(ModelMap model, @RequestParam("path") String path, @RequestParam(value="mode", required=false) String mode, @ModelAttribute("myBeanInSession") BeanInSession myBeanInSession, @ModelAttribute("getContent") GetContent getContent) { ... }
I have no problem.Code:public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { this.beanFactory.getBean("myBeanInSession"); ... }
But if I don't add codein the preHandle method of the interceptor I have the error :Code:this.beanFactory.getBean("myBeanInSession");
I also noticed that the instance of myBeanInSession is added in request.getSession().getAttribute("myBeanInSession ") by the method this.beanFactory.getBean("myBeanInSession");Session attribute "myBeanInSession" required - not found in Session
So it seems there is a relation between session-scoped beans, HttpSession.getAttribute() and @SessionAttributes annotation but I don't really understand how it works...
I wonder if it is not because of side effects that my code works but that it would be a better practice not to use @SessionAttributes annotation for referring to session-scoped beans. But then how could i refer session-scoped beans in my annotated controllers methods ?
Thanks
G. Dony


Reply With Quote