1

I want to create a initilisation file in my spring-mvc web project . What is the best way to do it so that i will be able to get thoses value in my java files and as well in my jsp files ? Thank you for any advise!

UPDATE

what if i wand to load thoses parameters from lets say an init.ini file and make them available all the time while the application is running?

storm_buster
  • 7,362
  • 18
  • 53
  • 75

2 Answers2

0

If you want to make those parameters available to all jsp files and controllers you can create an interceptor.

public class ParametersInterceptor extends HandlerInterceptorAdapter {


   Object parameters = null;
   public void loadParameters(){
      parameters = //Read from database or properties file.
   }

    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {

            request.setAttribute("parameters", parameters);
            return true;
    }

}

Then define your interceptor as a bean.

<bean name="parametersInterceptor"
    class="com.zzz.zzz.ParametersInterceptor"
    init-method="loadParameters" >
</bean>

Then in your urlMappping bean you can add this to tell spring that you want a list of interceptors to be executed for the defined mappings.

<property name="interceptors">
   <list>
    <ref bean="parametersInterceptor" />
   </list>
</property>

Here is an example using interceptors

Enrique
  • 9,920
  • 7
  • 47
  • 59
  • thank you for your answer, but in that way, in each request ,i'll will have to do the same action, i may be loosing performance. what if i wand to load thoses parameters from lets say an init.ini file and make them available all the time while the application is running? – storm_buster Dec 12 '10 at 00:11
  • @stunaz You are right. I have updated my answer. Notice the init-method="loadParameters" and the new loadParameters method. – Enrique Dec 12 '10 at 00:16
0

Most of the Spring webapps I work on have a properties file in src/main/resources that we access from both Java code as well as JSP files.

We use a custom taglib to load the property of interest like this:

<foo:propertyLoader bundle="bar" property="googleKey"/>

but there are many ways to read a properties file in JSP. See this stackoverflow question:

JSP/servlet read parameters from properties file?

We tend to store things like GoogleAPI keys, Google Analytic key, URIs to other web applications, etc in there.

Community
  • 1
  • 1
nickdos
  • 8,348
  • 5
  • 30
  • 47