Spring MVC - JSP - 存储环境特定常量的地方

wal*_*mon 7 java spring jsp spring-mvc

在Spring-MVC/JSP应用程序中,您将存储需要由控制器和视图访问的内容,例如特定于环境的base_url,要在javascript中使用的应用程序ID等等?

我已经尝试创建一个应用程序范围的bean,然后<jsp:useBean>在我的JSP的顶部,但这似乎没有工作.

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

并以下列方式使用它

<jsp:useBean id="myEnv" scope="application" type="com.myapp.MyAppEnvironment"/>
Run Code Online (Sandbox Code Playgroud)

ska*_*man 9

什么是scope="application"?这对我来说是新的.

无论如何,如果您只需要JSP可以访问Spring bean,那么您可以使用exposedContextBeanNames属性将bean公开给JSTL InternalResourceViewResolver.例如:

<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>
Run Code Online (Sandbox Code Playgroud)

然后在你的JSP中:

 ${myEnv.baseUrl}
Run Code Online (Sandbox Code Playgroud)