java - 资源包在 Spring 4.1 中不起作用

标签 java spring jsp spring-mvc

SocialgraphUI-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="socialgraphui.controller" />

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

        <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter" />
        </mvc:message-converters>
        </mvc:annotation-driven>

       <mvc:resources mapping="/scripts/**" location="/scripts/" />
       <mvc:resources mapping="/styles/**" location="/styles/" />
       <mvc:resources mapping="/fonts/**" location="/fonts/" />
       <mvc:resources mapping="/images/**" location="/images/" />

       <!-- Spring MVC Message Source -->
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="useCodeAsDefaultMessage" value="true"/>
            <property name="basenames">
                <list>
                    <value>content.socialGraph</value>
                </list>
            </property>
        </bean>

    </beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cz.ario</groupId>
<artifactId>socialgraphui</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>socialgraphui</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.1.0.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>  
        <groupId>com.google.code.gson</groupId>  
        <artifactId>gson</artifactId>  
        <version>2.2.4</version>  
    </dependency>  
    <dependency>
        <groupId>uk.com.robust-it</groupId>
        <artifactId>cloning</artifactId>
        <version>1.7.4</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>${spring.version}</version>
        <type>jar</type>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

资源包文件路径为src/main/resources/content/socialGraph.properties,这是该文件中的一个属性

socialgraph.panel.filter.time.header=Time

当我想在 .jsp 文件中使用此属性时,属性未映射,并且 h4 元素使用 .jsp 文件的源代码呈现:

<h4> <spring:message code="socialgraph.panel.filter.time.header" /> </h4> 

最佳答案

我正在使用 Spring 4,这就是我的做法

     <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

我的属性文件直接位于资源文件夹下。对于您的情况,您可能需要将 basename 的值更改为 classpath:content/socialGraph

关于java - 资源包在 Spring 4.1 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27365960/

相关文章:

java - Spring Boot 日志记录覆盖颜色

java - spring resttemplate url编码

java - 几分钟后,Spring Boot 停止解析 View

java - 启动 Apache Tomcat 7 时指定 JRE/JDK

java - 删除在 SWT 显示上创建的使用 GC 制作的绘图

java - 监控 JDBC 连接池

java - 我的 JList 模型方法给我一个错误

java - 如何在 WAS 6.0 上运行我的 JSF 1.1 应用程序

java - 如何在 Eclipse 中使用 pom.xml/Maven 初始化本地thoughtsite(App Engine示例)项目?

forms - 将表单提交到 Servlet 后,如何在 JSP 中保留 HTML 表单字段值?