Spring 3.1:从属性文件中拉出字符串

时间:2013-01-18 18:33:32

标签: java spring spring-mvc properties

我正在使用/学习Spring 3.1。我试图从* .properties文件中删除Strings。谷歌周围我找不到一个完整的例子。我设法将所有这些拼凑在一起。它编译和乐趣,但我没有得到任何价值。关于我所缺少的任何线索?

我的messages.properties文件名为 messages.properties 位于 war / WEB-INF / classes

test = From Messages Properties File

我的应用程序上下文文件名为 acme-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"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.1.xsd">

  <context:component-scan base-package="gov.noaa.acme.controller" />

  <mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/SDB/common/"/>
  <mvc:annotation-driven/>

  <!-- define the properties file to use -->
  <util:properties id = "messages" location="classpath:/messages.properties" />

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

  <bean name="af" class="gov.noaa.acme.controller.security.AuthenticationFilter"/>

  <!-- Configure the multipart resolver -->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000000"/>
  </bean>

</beans>

我的控制器类(简化)

package com.acme.controller;

import java.security.Principal;
import javax.servlet.http.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import org.apache.log4j.Logger;

@Controller
public class LoginController {

    @Value("#{messages['messages.test']}") private String test;
    private static final Logger logger  = Logger.getLogger(LoginController.class);

    @RequestMapping({"/","home"})
    public String home(ModelMap model,HttpSession session,HttpServletRequest request) {

        model.put("test",test);
        return "login";
    }

}// end class LoginController

我的log.jsp文件:(简化)

<%@ page language = "java" session = "true" import = "java.util.*, java.text.*" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix = "f" uri="http://www.springframework.org/tags/form"%>

test: ${test}

1 个答案:

答案 0 :(得分:2)

您应该使用:

@Value("#{messages['test']}")

而不是:

@Value("#{messages['messages.test']}")

如果您只有一个属性文件,也可以使用:

@Value("#{test}")