0

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

我的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}
4

1 回答 1

2

你应该使用:

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

代替 :

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

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

@Value("#{test}")
于 2013-01-18T19:00:01.867 回答