Contexto de aplicación nulo de primavera en JSP

Am trying to get the application context in my JSP page. I have the following scriptlet in JSP.

       WebApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()); 
       MyClass myClass = (MyClass)(appCtx.getBean("myClass"));

When using this a null pointer exception is getting thrown for the application context. What am I doing wrong here? I dont have a application-context.xml file because I dont need one. I have the usual dispatcher-servlet.xml with handler mappings.

Also, as an added info: I am trying to access this "myClass" object which is being injected using PropertyPlaceholderConfigurer which reads from external environmental variables and injects to "myClass". Any pointers?

Actualizar:

I tried using "RequestContextUtils" as suggested but now am getting "No WebApplicationContext found: not in a DispatcherServlet request" exception. The JSP in which I am trying to access the bean is the entry point of the application . My view resolver looks like this,

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    <property name="exposedContextBeanNames">
        <value>myClass</value>
    </property>
   </bean> 

The JSP am trying to access is outside of this "views" folder. Please advise

preguntado el 08 de noviembre de 11 a las 19:11

I don't do Spring MVC, but isn't just EL ${myClass} possible in Spring MVC? Java code doesn't belong in JSP anyway. -

Why are you trying to access the app context in the JSP? You're using MVC, anything for the view layer should be exposed through the controller. -

@BalusC: Only if view resolver is configured to expose beans as attributes. -

1 Respuestas

WebApplicationContextUtils.getRequiredWebApplicationContext() returns an application context associated with ContextLoaderListener (es decir applicationContext.xml).

To get a context associated with DispatcherServlet necesitas llamar RequestContextUtils.getWebApplicationContext(request).

Alternatively, you can configure InternalResourceViewResolver to expose beans as attributes. Then you can access beans in JSP as ${myClass}. You can expose all beans by setting exposeContextBeansAsAttributes a true (I think it incurs some performance penalty), or explicitly list beans to be exposed with exposedContextBeanNames.

respondido 08 nov., 11:23

Have updated my question based on this suggestion. Unfortunately it does not work in my case - raghav

@Srini: If so, you won't be able to access DispatcherServlet's context. It would be better to put all JSPs behind DispatcherServlet, just in case. It will solve your problem as well. To avoid writing trivial controllers you can use <mvc:view-controller> - axtavt

As axtavt had suggested, moving the JSP so that it is behind the front controller did the trick. Thanks to everyone for their suggestion - raghav

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.