JSP/servlet read parameters from properties file?

前端 未结 4 961
梦谈多话
梦谈多话 2020-12-31 22:34

My JSP pages need to display different information depending on which environment they\'re in (dev, production, sandbox, etc). I want to have a properties file for each of

相关标签:
4条回答
  • 2020-12-31 22:42
    • You can load the properties using java.util.Properties (or commons-configuration) in a ServletContextListener's contextInitialized(..) method.

    • register the listener with <listener> in web.xml

    • You then store the Properties into the ServletContext (you can get it from the event) (ctx.setAttribute("properties", properties)

    • then access the properties using ${applicationScope.properties.propName} (as BalusC noted, applicationScope is optional)

    Update:

    Initially I thought spring had some ready-to-use facility for that, but it turns out it's not exactly the case. You have two options:

    • this article explains something similar to my suggestion above, but using spring's PropertyPlaceholderConfigurer

    • this answer and this answer allow you to expose all your beans, including a PropertyPlaceholderConfigurer to the servlet context.

    0 讨论(0)
  • 2020-12-31 22:48

    I had the same problem. I was able to solve it by exposing the config file in my webmvc-config.xml file using a Resource Bundle:

    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="classpath:META-INF/spring/config" p:fallbackToSystemLocale="false"/>
    

    Then I was able to use it in my JSPs using a <spring:message /> tag:

    <spring:message code="reactor.appKey" var="reactorAppKey" />
    <scrip data-app-key="${reactorAppKey}"></script>
    
    0 讨论(0)
  • 2020-12-31 22:49

    Without going into the discussion of where is the best location to store environment-specific data (hint: certainly not property files), I would stick to the basics:

    JSP pages render data; they don't fetch it. If at some point, later on, you end up getting properties from somewhere else other than a property file - your JSP, assuming your application is well-designed - should not change.

    Therefore, the first approach you mentioned makes sense (assuming, again, that you'd like to stick with environment-specific information being read from Property files).

    0 讨论(0)
  • 2020-12-31 22:54
    1. Place the properties file in a known location within your project. For example: /WEB-INF/config/environment.properties.
    2. Create a simple java bean class that has getters (accessors if you like) exposing each of the desired properties (I will refer to this as the PropertyExposer class). For example: PropertyExposer.getEnvironmentName()
    3. In a startup class (maybe a SessionContextListener or a servlet with a low <load-on-startup> value) load the properties, create your PropertyExposer object, and store it in session (or application based on your needs) scope.

    After doing the above, your properties will be available to your JSP files.

    If you initialize using a SessionContextListener (this is not valid code, but the point is made):

    
    SessionContextListenser.contextInitialized(ServletContextEvent event)
    {
        event.getServletContext().setAttribute();  // set application scope value.
    }
    
    

    If you initialize using a Servlet (assuming you extend GenericServlet):

    
    YourServletClass.init()
    {
        getServletContext().setAttribute(); // set application scope value.
    }
    
    
    0 讨论(0)
提交回复
热议问题