【问题标题】:Resource and properties file management issue资源和属性文件管理问题
【发布时间】:2015-02-22 14:09:21
【问题描述】:

我正在努力学习Spring。我在管理我的资源和属性文件时遇到了一些问题。

这是我的mvc-dispatcher-servlet.xml 文件。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.test.common.controller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="messages/messages">
        </property>
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang">
        </property>
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="tr">
        </property>
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor"/>
        </property>
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
</beans>

这是我的文件描述符。

我的问题是:

当我删除时

<mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />

来自mvc-dispatcher-servlet.xml,无法加载文件header.css

但是不删除该行会阻止我更改语言。我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: java spring resources properties-file


    【解决方案1】:

    您应该将您的 css、js 和图像资源映射到 web.xml 中的默认 servlet:

            <servlet>
                    <servlet-name>default</servlet-name>
                    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
            </servlet>
            <servlet-mapping>
                    <servlet-name>default</servlet-name>
                    <url-pattern>/resources/img/*</url-pattern>
            </servlet-mapping>
            <servlet-mapping>
                    <servlet-name>default</servlet-name>
    
                    <url-pattern>*.css</url-pattern>
            </servlet-mapping>
            <servlet-mapping>
                    <servlet-name>default</servlet-name>
                    <url-pattern>*.js</url-pattern>
            </servlet-mapping>
    

    如果这对您没有帮助,请您提供您的应用程序 web.xml 文件内容。

    附:我添加了带有资源和 i18n 支持的 example application。您可以通过以下方式进行检查: http://localhost:8080?locale=en

    http://localhost:8080?locale=ua

    【讨论】:

    • 这绝对可以正常工作。非常感谢您的帮助
    • 这可行,但我无法使用图像。我使用 "" 显示图像,但未加载。
    • 请仔细检查您正在使用的 web.xml 和路径中的映射。我更新了我的例子,大家可以参考一下。
    猜你喜欢
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多