2

I am using Spring. A properties placeholder is used to access all properties in the application. I would like to use it also in JSP pages.

I found some solutions using Spring-MVC but I do not use it. I uses a org.apache.jasper.servlet.JspServlet that I could overwrite in my web.xml if necessary.

Is it possible to somehow expose the properties to the JSP code without having to overwrite the ServletContextListener class ? (like it was done in this article)

Olivier.Roger
  • 4,241
  • 5
  • 40
  • 68

1 Answers1

3

Inject the property into your controller/servlet, then add that value to your model. Access that attribute as you would any other :

@Value("${myProperty.setting}")
private String whateverYouWantToCallIt;

then :

model.addAttribute("mySetting", whateverYouWantToCallIt);

and in JSP :

<span>${mySetting}</span>

And if its just a text message used in lots of places you could use message.properties.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • I do not use any controller (no Spring-MVC), it is just plain JSP. – Olivier.Roger Apr 02 '12 at 10:22
  • why is your question tagged spring then ? – NimChimpsky Apr 02 '12 at 10:24
  • I would like to inject the PropertyPlaceHolder from my Spring configuration in the Servlet in order to use spring properties in my JSP pages. I do not know if Spring provides an easy way to do that out-of-the-box. – Olivier.Roger Apr 02 '12 at 10:30
  • inject it as I said, then set session attribute instead of adding to model – NimChimpsky Apr 02 '12 at 10:39
  • I used a `ServletContextListener` to inject all the properties as ApplicationContext attributes. However, when I use `${mySetting}` the token is not replaced by its value. What component is responsible to perform this transformation ? (It works when I call `this.getServletConfig().getServletContext().getAttribute("mySetting");`) – Olivier.Roger Apr 02 '12 at 14:41
  • 1
    If the property is in the session, as @NimChimpsky suggested in his comment, you should use `${sessionScope.myStting}` – jddsantaella Apr 02 '12 at 17:22
  • I used methods as indicates in http://stackoverflow.com/questions/123657/how-can-i-share-a-variable-or-object-between-two-or-more-servlets – Olivier.Roger Apr 03 '12 at 08:13
  • I succedded to use ${mySetting} by enabling EL using `<%@ page isELIgnored="false" %>` as shown in http://stackoverflow.com/questions/1038046/enabling-el-in-jsp – Olivier.Roger Apr 03 '12 at 12:12