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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17022911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:00:19  来源:igfitidea点击:

How to get properties in JSP files using spring mvc 3

spring

提问by user2432330

I am very new to spring mvc 3 annotation based application. I have two properties files - WEB-INF\resources\general.properties, WEB-INF\resources\jdbc_config.properties

我对基于 spring mvc 3 注释的应用程序很陌生。我有两个属性文件 - WEB-INF\resources\general.properties, WEB-INF\resources\jdbc_config.properties

Now I want to configure them through spring-servlet.xml. How I can achieve this?

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

In general.properties,

在general.properties中,

label.username = User Name:
label.password = Password:
label.address = Address:

...etc jdbc_config.properties,

...等 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

---etc

- -等等

If I want to get label.username and app.jdbc.driverClassName in my jsp page, how do I code for them?

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

I also want to access these properties values from my service. How to get these property values using respective keys in method level in service class or controller class?

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

回答by Pavel Horal

You need to distinguish between application properties (configuration) and localisation messages. Both use JAVA properties files, but they serve different purpose and are handled differently.

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

Note:I am using Java based Spring configuration in the examples bellow. The configuration can be easily made in XML as well. Just check Spring's JavaDocand reference documentation.

注意:我在下面的示例中使用了基于 Java 的 Spring 配置。配置也可以很容易地在 XML 中进行。只需查看 Spring 的JavaDoc参考文档



Application Properties

应用程序属性

Application properties should be loaded as property sourceswithin your application context. This can be done via @PropertySourceannotation on your @Configurationclass:

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

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

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

}

Then you can inject properties using @Valueannotation:

然后你可以使用@Value注解注入属性:

@Value("${my.config.property}")
private String myProperty;


Localisation Messages

本地化消息

Localisation messages is a little bit different story. Messages are loaded as resource bundlesand a special resolution process is in place for getting correct translation message for a specified locale.

本地化消息有点不同。消息作为资源包加载,并且有一个特殊的解析过程来为指定的语言环境获取正确的翻译消息。

In Spring, these messages are handled by MessageSources. You can define your own for example via ReloadableResourceBundleMessageSource:

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

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

You can access these messages from beans if you let Spring inject MessageSource:

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

@Autowired
private MessageSource messageSource;

public void myMethod() {
    messageSource.getMessage("my.translation.code", null, LocaleContextHolder.getLocale());
}

And you can translate messages in your JSPs by using <spring:message>tag:

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

<spring:message code="my.translation.code" />

回答by Taoufik Mohdit

Refer to @Pavel Horal's answer

请参阅@Pavel Horal 的回答

回答by Jonghee Park

I ended up using Environment

我最终使用了环境

Add these lines to config

将这些行添加到配置

@PropertySource("classpath:/configs/env.properties")
public class WebConfig extends WebMvcConfigurerAdapter{...}

You can get the properties from controller using autowired Environment

您可以使用自动装配环境从控制器获取属性

public class BaseController {
    protected final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public Environment env;

    @RequestMapping("/")
    public String rootPage(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) {
        LOG.debug(env.getProperty("download.path"));
        return "main";
    }
}

回答by Vahap Gencdal

Firstly import spring tag lib:

首先导入 spring 标签库:

<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>  

Than import property from your application.properties

比从 application.properties 导入属性

<spring:eval var="registration_url" expression="@environment.getProperty('service.registration.url')"/>  

Than use your variable

比使用你的变量

<a href="<c:out value="${registration_url}"/>" class="btn btn-primary btn-block"> test </a>