0

I have the following configuration in Spring application context.

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="props">
            <list>
                <value>file://${user.home}/myConfig.properties</value>
            </list>
        </property>
    </bean>

let's say I want to display a value (e.g : app.url.secret) defined as a property in the myConfig.properties file, directly in a jsp. How can I achieve that ?

thanks in advance for your help

kaffein
  • 1,766
  • 2
  • 28
  • 54
  • possible duplicate of [How to use property from property file specified in PropertyPlaceholderConfigurer in JSP](http://stackoverflow.com/questions/3933862/how-to-use-property-from-property-file-specified-in-propertyplaceholderconfigure) – sinuhepop Jul 18 '13 at 15:19

2 Answers2

0

You will have to get it to your model in some way:

One approach will be to use a PropertyHolder this way:

@Component
public class PropertyHolder {

  @Value("${myprop}")
  private String myProperty;

  //getters and setters..

}

In your controller:

   @Controller
   public class MyController {
      @Autowired private PropertyHolder propertyHolder;

      @ModelAttribute
      public void setModelAttributes(Model model) {
        model.put("myprops", propertyHolder);
      } 

....rest of your controller..

   }

then you have access to myprops in your jsp - myprops.myProperty

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Thanks for your response but isn't there any other way of accessing it directly ? I think having to create and then add a reference to the property holder in a controller (which may have nothing to do with the jsp) is a little bit too much for just accessing a property. – kaffein Jul 18 '13 at 14:23
  • No, doubt if there is a direct way other than through the model populated through the controller. – Biju Kunjummen Jul 18 '13 at 14:35
0

Firstly populate your model with the property value on your controller, then return a view that resolves to your JSP

You can use @Value annotation to inject the property to your controller

@Controller
public class MyController {

  @Value("${app.url.secret}") private String urlSecret;

  @RequestMapping("/hello")
  public String hello(Model model) {
    model.addAttribute("urlSecret", urlSecret);

    // assuming this will resolve to hello.jsp
    return "hello";
  }
}

Then on your hello.jsp

<%@ page ... %>
<html>
 ...
 The secret url is: ${urlSecret}
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Thanks for your response. Would it be possible to directly inject the bean representing the PropertySourcesPlaceholderConfigurer in the jsp and perhaps using the SpEL to get the value out of it ? – kaffein Jul 18 '13 at 14:27
  • Maybe yes.. Have a look at `` tag (if I'm not wrong). Read more on the i18n section of spring manual – gerrytan Jul 18 '13 at 14:29
  • humm but is for i18n as you said :) that would be weird to having to use it for that. thanks anyway. – kaffein Jul 18 '13 at 14:31
  • But the i18n mechanism is precisely what you're trying to achieve. So although you don't need to i18n you can still directly display message from properties file – gerrytan Jul 18 '13 at 14:33