Page 3 of 3 FirstFirst 123
Results 21 to 22 of 22

Thread: Problem with @SessionAttributes and @ModelAttribute

  1. #21
    Join Date
    Jan 2009
    Posts
    2

    Default

    Hi,

    I have in my spring configuration file :

    - a bean which is session-scoped

    Code:
    <!-- **************	-->
    	<!--	context		-->
    	<!-- **************	-->
    	<bean id="myBeanInSession" class="blabla.BeanInSession"
    	scope="session">
    	</bean>
    - an interceptor :
    Code:
    <!-- ************ -->
    	<!--  interceptor -->
    	<!-- ************ -->
    	<bean name="securityInterceptor" class="web.interceptor.SecurityInterceptor">
    	</bean>
    I have annotate-based controllers like this :
    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) {
    ...
    }
    If in the securityInterceptor i put the code :

    Code:
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
    			Object arg2) throws Exception {
    		this.beanFactory.getBean("myBeanInSession");
    ...
    }
    I have no problem.

    But if I don't add code
    Code:
    this.beanFactory.getBean("myBeanInSession");
    in the preHandle method of the interceptor I have the error :

    Session attribute "myBeanInSession" required - not found in Session
    I also noticed that the instance of myBeanInSession is added in request.getSession().getAttribute("myBeanInSession ") by the method this.beanFactory.getBean("myBeanInSession");

    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

  2. #22
    Join Date
    Jan 2009
    Posts
    2

    Default

    Ok I finally forget @SessionAttributes annotation and HttpSession.getAttributes() or WebRequest.getAttributes().

    My session-scoped bean is declared in my spring xml configuration file like this :

    Code:
    <bean id="myBeanInSession" class="blabla.BeanInSession" scope="session">
    		<aop:scoped-proxy/>
    	</bean>
    And in my annotated controller I encapsulate ma session-scoped bean like this :

    Code:
     private BeanInSession beanInSession;
    	
    	@Autowired
    	public GetContentController(BeanInSession beanInSession) {
    		this.beanInSession= beanInSession;
    	}
    Now I can use my session-scoped bean in any method of my controller.

    But I still wonder why when using WebRequest.getAttributes("myBeanInSession" , SESSION_SCOPE) (or HttpSession.getAttribute("myBeanInSession")) the bean is not accessible except after invoking beanFactory.getBean(). Is there a lazy initialisation of session-scoped beans ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •