14

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

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

In general.properties,

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

...etc 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?

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?

bodyjares
  • 440
  • 1
  • 4
  • 16
user2432330
  • 145
  • 1
  • 3
  • 8
  • Possible duplicate of [How to show values from property file in JSP in a spring MVC app](http://stackoverflow.com/questions/15111260/how-to-show-values-from-property-file-in-jsp-in-a-spring-mvc-app) – Vadzim Mar 16 '16 at 20:30

3 Answers3

17

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.

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 JavaDoc and reference documentation.


Application Properties

Application properties should be loaded as property sources within your application context. This can be done via @PropertySource annotation on your @Configuration class:

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

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

}

Then you can inject properties using @Value annotation:

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

Localisation Messages

Localisation messages is a little bit different story. Messages are loaded as resource bundles and 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:

@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:

@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 code="my.translation.code" />
Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • 1
    thanks for notifying me diff. between application properties (configuration) and localisation messages ... please update how to access **value** from **application properties** in jsp, – Shantaram Tupe Aug 21 '17 at 06:15
  • **if your messages file is located outside .jar** :: assume you have file path/to/messages_en.properties on base name you have to set : messageSource.setBasename("file:path/to/messages") omitting the suffix _en.properties – Mike D3ViD Tyson Jul 25 '18 at 15:27
  • easiest way is to use – JJ Roman Apr 13 '20 at 19:25
4

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";
    }
}
Jonghee Park
  • 1,257
  • 15
  • 14
4

Firstly import spring tag lib:

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

Than import property from your 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>
Vahap Gencdal
  • 1,900
  • 18
  • 17