Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Accessing a session-scoped bean from dispatcher-servlet.xml in my views

  1. #1
    Join Date
    May 2011
    Posts
    8

    Default Accessing a session-scoped bean from dispatcher-servlet.xml in my views

    I have configured a session-scoped bean in my dispatcher config:

    Code:
    <bean id="mySessionScopedBean" class="net.sandbox.pojos.MySessionScopedBean" scope="session">
        <aop:scoped-proxy />
    </bean>
    If I want to access the data inside this bean from my views, I have to explicitly do model.addAttribute(mySessionScopedBean) from my controllers every time I want to show information from the session-scoped bean in a view. (Provided, of course, that the controller has @Autowired'd the bean.)

    I thought I had found a trick to automatically make the session-scoped bean accessible for a JSP page.

    Code:
    <jsp:useBean id="mySessionScopedBean" scope="session" class="net.sandbox.pojos.MySessionScopedBean" />
    But this doesn't seem to use the Spring-created instance of the session-scoped bean. Instead, it appears that a separate session-scoped bean was created. Is there a way to access the session-scoped bean that was defined in my dispatcher-servlet.xml in my JSP pages without having to pass it to the view using model.addAttribute each time?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Although I wouldn't suggest doing it this way (and simply put it in the model) you can use the RequestContextUtils to get access to the applicationcontext. With that you can access the bean you want by simply calling getBean...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2011
    Posts
    8

    Default

    I'm looking at RequestContextUtils.getWebApplicationContext(Servl etRequest request) and WebApplicationContext.getBeansOfType(Class<T> type) to do the job. Is this a good way to do it? If not, can you point me to a code sample that shows me how to do it?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    As stated simply use the RequestContextUtils to get the ApplicationContext and use that to retrieve the bean. As I also stated I would strongly recommend against it, instead I probably would write a HandlerInterceptor which adds this bean to the model or as a request attribute. You should keep your views as lean as possible.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    May 2011
    Posts
    8

    Default

    I downloaded an example of a project that uses HandlerInterceptor for this. Here's what I came up with...

    I added this to dispatcher-servlet.xml.
    Code:
    <bean id="sessionBeanInserter" class="net.sandbox.handlerinterceptors.SessionBeanInserter" />
    Here's my net.sandbox.handlerinterceptors.SessionBeanInserte r.
    Code:
    public class SessionBeanInserter extends HandlerInterceptorAdapter {
        @Autowired
        private MySessionScopedBean mySessionScopedBean;
        
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            // Insert the session-scoped bean
            modelAndView.addObject("mySessionScopedBean", mySessionScopedBean);
        }
    }
    This results in a server error. The exception is as follows:

    Code:
    javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
            ...
    Root cause:

    Code:
    java.lang.NoClassDefFoundError: javax/portlet/PortletRequest
    	java.lang.Class.getDeclaredConstructors0(Native Method)
    	java.lang.Class.privateGetDeclaredConstructors(Class.java:2406)
            ...
    What am I doing wrong?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Judging from the exception you use the wrong HandlerInterceptor the one from the portlet instead of the servlet package... However I suggest posting the full stacktrace that should give more information.

    In short you have introduced a dependency to the portlet stuff somewhere...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    May 2011
    Posts
    8

    Default

    Here's the full stack trace.

    Exception

    Code:
    javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
    	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
    	org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1714)
    	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	java.lang.Thread.run(Thread.java:636)
    Root cause
    Code:
    java.lang.NoClassDefFoundError: javax/portlet/PortletRequest
    	java.lang.Class.getDeclaredConstructors0(Native Method)
    	java.lang.Class.privateGetDeclaredConstructors(Class.java:2406)
    	java.lang.Class.getDeclaredConstructors(Class.java:1853)
    	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:227)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:930)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:903)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    	javax.servlet.GenericServlet.init(GenericServlet.java:160)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
    	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
    	org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1714)
    	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	java.lang.Thread.run(Thread.java:636)
    Root cause
    Code:
    java.lang.ClassNotFoundException: javax.portlet.PortletRequest
    	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    	java.lang.Class.getDeclaredConstructors0(Native Method)
    	java.lang.Class.privateGetDeclaredConstructors(Class.java:2406)
    	java.lang.Class.getDeclaredConstructors(Class.java:1853)
    	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:227)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:930)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:903)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    	javax.servlet.GenericServlet.init(GenericServlet.java:160)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
    	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
    	org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1714)
    	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	java.lang.Thread.run(Thread.java:636)

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Judging from the stacktrace it looks like one of your classes has a dependency on the PortletRequest... I suggest checking your constructors and/or checking your imports. It might even be that you use something else that has a dependency on the PortletRequest.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    May 2011
    Posts
    8

    Default

    Apparently javax.portlet isn't a default part of the servlet API, so I downloaded http://repository.jboss.org/portlet/...ortlet-api.jar. I still can't access the session-scoped object from my view though...

    Code:
    MySessionScopedBean mySessionScopedBean = (MySessionScopedBean) request.getAttribute("mySessionScopedBean");
    out.println("[DEBUG] " + mySessionScopedBean);
    This outputs [DEBUG] null.

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    As I stated there must be something which has a dependency on the portlet api portlet != servlet.. .I suggest a read on what a portlet is... Judging from what you tell you don't need the portlet api and thus you are using something wrong in your application (something which is dependend on the portlet api).

    Figure out the error instead of slapping jars in there which won't solve your problem.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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