Spring MVC Webapp:在哪里存储常见图像的路径? [英] Spring MVC Webapp: Where to store paths to common images?

查看:98
本文介绍了Spring MVC Webapp:在哪里存储常见图像的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个以Tiles/JSP作为视图技术的Spring MVC Web应用程序. 以前,我将通用图像的路径存储在Common中:

I'm building a Spring MVC web application with Tiles/JSP as the view technology. Previously I stored the paths to common images in class Common:

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

然后我将在jsp中使用此类来获取图像src,例如

Then I would use this class in jsp to get the image src like

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

我想摆脱我的jsp代码中的scriptlet,而改用jstl等. 存储此类信息的最佳方法是什么?是资源包吗? 您如何解决这个问题?

I would like to get rid of scriptlets in my jsp code and use jstl etc. instead. What is the best way to store this kind of information? Is it resource bundles? How have you solved this?

推荐答案

最后,我使用了Spring的主题支持来实现我想要的. 在我的查看代码中,我使用<spring:theme code=""/>标记获取图像文件的路径:

In the end I used Spring's theme support to achieve what I wanted. In my view code I use the <spring:theme code=""/> tag to get the path to image file:

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

此标记的行为类似于任何<fmt:message><spring:message>标记,但是它具有自己的消息包".我的applicationContext中的必要配置是:

This tag behaves like any <fmt:message> or <spring:message> tag, but it has its own "message bundles". The necessary configurations in my applicationContext are:

 <!-- 
    ========================================================= 
    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中 看起来像这样:

All themes of my application are stored under /WEB-INF/classes/themes/. The default theme properties are in /WEB-INF/classes/themes/default.properties It looks like this:

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

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

To change the theme (and icons) of my app I use a ThemeChangeInterceptor (in 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"请求参数切换主题.

This enables the user to switch the theme via a "&theme=themes.default" or "&theme=themes.alternative" request parameter.

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

One key part of my setup is the @contextPath@ in the theme properties file. This is replaced during the Ant build process with the correct context path for development/testing/production environment. The key part of my build.xml is:

    <!-- 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应用程序主题的开始".在我看来,此设置使更改应用程序的外观和感觉变得非常容易.

I hope this gives you a "running start" on Spring web app themes. In my opinion this setup makes it quite easy to alter the look and feel of an application.

参考文献:

  • Spring themes: http://static.springframework.org/spring/docs/3.0.x/spring-framework-reference/html/ch16s07.html
  • FamFam icons: http://www.famfamfam.com/lab/icons/silk/
  • Ant copy task: http://ant.apache.org/manual/Tasks/copy.html

这篇关于Spring MVC Webapp:在哪里存储常见图像的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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