Spring MVC - JSP - a place to save specific environmental conditions

Where in the Spring-MVC / JSP application should you store things that should be accessible to both controllers and views, such as environment-specific base_url, application identifiers that will be used in javascript, etc.?

I tried to create a scoped bean application and then <jsp:useBean> at the top of my JSPs, but this does not seem to work.

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

And using it as follows

 <jsp:useBean id="myEnv" scope="application" type="com.myapp.MyAppEnvironment"/> 
+7
java spring spring-mvc jsp
source share
1 answer

What is scope="application" ? This is new to me.

In any case, if you need your JSPs to access Spring beans, you can open the beans in JSTL using the exposedContextBeanNames InternalResourceViewResolver property. For example:

 <bean id="myEnv" class="com.myapp.MyAppEnvironment"> <property name="baseUrl" value="http://localhost:8080/myapp/"/> <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} 
+9
source share

All Articles