Spring MVC - JSP - 存储环境特定常量的位置 [英] Spring MVC - JSP - Place to Store Environment Specific Constants

查看:126
本文介绍了Spring MVC - JSP - 存储环境特定常量的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我已经尝试在我的JSP顶部创建一个应用程序作用域bean然后< jsp:useBean> ,但这并不是'似乎工作正常。

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/"/>
        <property name="videoPlayerId" value="234346565"/>
    </bean>

并按以下方式使用

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


推荐答案

什么是 scope =应用?对我来说这是一个新的。

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

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

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/"/>
    <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>

然后在你的JSP中:

 ${myEnv.baseUrl}

这篇关于Spring MVC - JSP - 存储环境特定常量的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆