views:

127

answers:

1

Where in the Spring-MVC/JSP application would you store things that need to be accessed by both the controllers and views such as environment specific base_url's, application ids to be used in javascript and so on?

I've tried creating an application scoped bean and then <jsp:useBean> at the top of my JSPs, but that doesn't seem to be working.

   <!-- Environment -->
    <bean id="myEnv" class="com.myapp.MyAppEnvironment" scope="application">
        <property name="baseUrl" value="http://localhost:8080/myapp/"/&gt;
        <property name="videoPlayerId" value="234346565"/>
    </bean>

And using it in the following manner

<jsp:useBean id="myEnv" scope="application" type="com.myapp.MyAppEnvironment"/>
+1  A: 

What is scope="application"? That's a new one to me.

Anyway, if all you need is for your JSPs to be able to access Spring beans, then you can expose the beans to JSTL using the exposedContextBeanNames property of InternalResourceViewResolver. For example:

<bean id="myEnv" class="com.myapp.MyAppEnvironment">
    <property name="baseUrl" value="http://localhost:8080/myapp/"/&gt;
    <property name="videoPlayerId" value="234346565"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposedContextBeanNames">
      <list>
         <value>myEnv</value>
      </list>
   </property>
</bean>

and then in your JSP:

 ${myEnv.baseUrl}
skaffman
scope="application" - http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.htmlThat's an interesting feature of the view resolver, I'll try this and bump up the answer if it works for me, thanks
walnutmon
@jboyd: Ah, that's a JSP thing, it doesn't apply to Spring.
skaffman