我正在使用 Tiles/JSP 作为视图技术构建 Spring MVC Web 应用程序。之前我将常见图像的路径存储在 Common 类中:

 public final static String IMG_BREADCRUMBS_NEXT = "/shared/images/famfam/bullet_arrow_right.png";

然后我会在jsp中使用这个类来获取图像src,例如

 <img src="<%= Common.IMG_BREADCRUMBS_NEXT %>"/>

我想摆脱 jsp 代码中的 scriptlet 并使用 jstl 等。反而。存储此类信息的最佳方式是什么?是资源包吗?你是怎么解决这个问题的?

有帮助吗?

解决方案

最终我使用Spring的主题支持来实现我想要的。在我看来代码我使用 <spring:theme code=""/> 标签来获取图像文件的路径:

 <img src="<spring:theme code="theme.images.actions.edit.link"/>" />

该标签的行为与任何 <fmt:message> 或者 <spring:message> 标签,但它有自己的“消息包”。我的 applicationContext 中的必要配置是:

 <!-- 
    ========================================================= 
    Themes
    =========================================================
  -->
<bean id="themeResolver" class="org.springframework.web.servlet.theme.SessionThemeResolver">
    <property name="defaultThemeName" value="themes.default"/>
</bean>
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource" />    

我的应用程序的所有主题都存储在 /WEB-INF/classes/themes/. 。默认主题属性位于 /WEB-INF/classes/themes/default.properties它看起来像这样:

 ...
 theme.images.actions.show.link=/@contextPath@/shared/images/famfam/zoom.png
 theme.images.actions.delete.link=/@contextPath@/shared/images/famfam/cross.png
 ...

要更改应用程序的主题(和图标),我使用 ThemeChangeInterceptor (在 applicationContext 中)

<!--
========================================================= 
Theme resolving
=========================================================
--> 
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <property name="paramName" value ="theme" />
</bean>

这使得用户可以通过 "&theme=themes.default" 或者 "&theme=themes.alternative" 请求参数。

我的设置的一个关键部分是 @contextPath@ 在主题属性文件中。在 Ant 构建过程中,它会被替换为开发/测试/生产环境的正确上下文路径。我的 build.xml 的关键部分是:

    <!-- copy all common themes to classes -->
    <copy todir="${build.war}/WEB-INF/classes/themes" overwrite="true" filtering="true">
        <fileset dir="resources/themes" includes="**/*.properties" />
        <filterchain>
           <replacetokens>
                <token key="contextPath" value="${setup.contextPath}"/>
            </replacetokens>
        </filterchain>
    </copy>

我希望这能让您在 Spring Web 应用程序主题上有一个“良好的开端”。在我看来,这种设置使得改变应用程序的外观和感觉变得非常容易。

参考:

其他提示

使用在一个应用范围的配置Bean,所以你可以写类似

<img src="${configuration.imagePath}/icon.png">

我不使用Spring,但你也许可以使用依赖注入做同样的事情,以我们在JBoss和Seam的事情。

基本上,我们有所谓的配置,其属性是应用程序的配置参数,从XML配置加载的POJO类(实际上一个JBoss的MBean,但这是题外话)。在这个例子中,我们的豆将具有getImagePath()方法。

Seam将采取实例在“应用”范围的配置Bean的单个实例的护理,使它总是可用在表达式中使用,像上面的

岂不是更稳健的使用该确定基于数据库的主题的一类。这将允许用户根据时间或用户代理来管理主题,甚至实现主题?

有没有可能做到这一点,并在同一时间使用春天的主题,保存主题在用户会话?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top