如何使用spring mvc 3获取JSP文件中的属性

use*_*330 12 spring

我是一个非常新的基于mvc 3注释的应用程序.我有两个属性文件 - WEB-INF\resources\general.properties,WEB-INF\resources\jdbc_config.properties

现在我想通过spring-servlet.xml配置它们.我怎么能做到这一点?

一般来说,性能,

label.username = User Name:
label.password = Password:
label.address = Address:
Run Code Online (Sandbox Code Playgroud)

...等jdbc_config.properties,

app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost:[port_number]/
app.jdbc.username=root
app.jdbc.password=pass
Run Code Online (Sandbox Code Playgroud)

- -等等

如果我想在我的jsp页面中获取label.username和app.jdbc.driverClassName,我该如何为它们编码?

我还想从我的服务中访问这些属性值.如何使用服务类或控制器类中的方法级别的相应键获取这些属性值?

Pav*_*ral 17

您需要区分应用程序属性(配置)和本地化消息.两者都使用JAVA属性文件,但它们用于不同的目的,并且处理方式不同.

注意:我在下面的示例中使用基于Java的Spring配置.配置也可以用XML轻松完成.只需查看Spring的JavaDoc参考文档.


应用属性

应用程序属性应作为应用程序上下文中的属性源加载.这可以通过@PropertySource@Configuration班上的注释来完成:

@Configuration
@PropertySource("classpath:default-config.properties")
public class MyConfig  {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用@Value注释注入属性:

@Value("${my.config.property}")
private String myProperty;
Run Code Online (Sandbox Code Playgroud)

本地化消息

本地化消息是一个有点不同的故事.消息作为资源包加载,并且具有特殊的解决过程以获取指定区域设置的正确翻译消息.

在Spring中,这些消息由MessageSources处理.您可以通过ReloadableResourceBundleMessageSource以下方式定义自己的:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("/WEB-INF/messages/messages");
    return messageSource;
}
Run Code Online (Sandbox Code Playgroud)

如果让Spring注入,你可以从bean访问这些消息MessageSource:

@Autowired
private MessageSource messageSource;

public void myMethod() {
    messageSource.getMessage("my.translation.code", null, LocaleContextHolder.getLocale());
}
Run Code Online (Sandbox Code Playgroud)

您可以使用<spring:message>标记在JSP中翻译消息:

<spring:message code="my.translation.code" />
Run Code Online (Sandbox Code Playgroud)


Tao*_*dit 15

我假设您的文件spring-servlet.xml是您的应用程序上下文文件之一.如果是这种情况,请将以下bean添加到其中:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>WEB-INF/resources/general</value>
            <value>WEB-INF/resources/jdbc_config</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

那么这就是你如何在JSP中获取属性值(不要忘记在JSP的顶部添加spring taglib):

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<table>
    <tr>
        <td>User Name:</td>
        <td><spring:message code="label.username" />:</td>
    </tr>
    <tr>
        <td>Driver Class Name:</td>
        <td><spring:message code="app.jdbc.driverClassName" />:</td>
    </tr>
etc...
</table>
Run Code Online (Sandbox Code Playgroud)

  • Downvoting作为这个答案包含非常非常糟糕的做法,没有任何警告......为什么要将应用程序属性加载为消息?显然,这个问题值得更深入地回答有关属性源,占位符配置器以及应用程序属性和消息资源包之间的区别. (5认同)
  • @TaoufikMohdit您是否在第一句话后阅读了我的评论?应用程序属性不是消息.这些应该通过*property sources*和*placeholder configurers*加载. (2认同)