I am using Spring 3.0.5 and InternalResourceViewResolver to resolving the views. Now if I put all the .jsp files into WEB-INF/pages/view and use configuration like:
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/view</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
the pages resolving correctly.
But for different module I want to put my .jsp files in separate folders. For example package WEB-INF/pages/view/booking and WEB-INF/pages/view/quote will contain .jsp files for Booking and Quote module correspondingly.
How can I do such configuration ?
I'm not sure such configuration is easily possible. But there is another easy approach to having your views in different directories. Just use the common part of the path as prefix (/WEB-INF/pages/view/) in your view resolver configuration and then return the relative path of your view to the specified prefix from your controller:
booking/home -> will be resolved to /WEB-INF/pages/view/booking/home.jsp
quote/home -> will be resolved to /WEB-INF/pages/view/quote/home.jsp
Related
how to add spring tiles configuration feature through xml?...for example
suppose If I've 1000+ tiles resolver xml files.how can I add those xml files ? I need to add all the xml files or is there any new feature to inject all files?
You don't need to configure each view definition file. TilesConfigurer definitions property support wildcard characters.
In the following example TilesConfigurer will try to load all views.xml under any subpath of /WEB-INF/views/
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/layouts/layouts.xml</value>
<!-- Scan views directory for Tiles configurations -->
<value>/WEB-INF/views/**/views.xml</value>
</list>
</property>
</bean>
Hope this helps!
I want to load multiple properties files using <util:properties> tag in a spring 3 application.
I searched on the blogs, but cannot get the correct path to do this.
Hopefully somebody give me the answer to overcome this problem.
Actually <util:properties> is just convenient tag for org.springframework.beans.factory.config.PropertiesFactoryBean. And PropertiesFactoryBean does support multiple locations.
So it is possible to create bean with Properties this way:
<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:myprops-common.properties</value>
<value>classpath:myprops-override.properties</value>
<value>classpath:some-more-props-here.properties</value>
</list>
</property>
</bean>
My solution
<context:property-placeholder location="classpath*:*.properties,file:/some/other/path/*.properties" />
util:properties seems to support only 1 properties file (reference). You might want to use the configuration suggested by #peperg.
Hi guys is there any good examples of changing spring properties files content dynamically? I would really appreciate it if you could give me some example or link.
Thanks alot
I think you could use ReloadableResourceBundleMessageSource .It uses java.util.Properties instances as its internal data structure for messages.
Also , as the name suggests , this class supports reloading of properties files through the cacheSeconds setting, and also through programmatically clearing the properties cache. Note that since application servers do typically cache all files loaded from the classpath, you have to put properties files outside of your classpath (WEB-INF/classes) or it'll be cached and won't work.
References / examples / links
http://techdive.in/spring/spring-internationalization-i18n
http://www.jroller.com/raible/entry/spring_mvc_s_reloadableresourcebundlemessagesource
Actually, spring support ${variable} in configration file like below
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${XXX}/XXX.properties</value>
<value>file:${XXX}/YYY.properties</value>
</list>
</property>
</bean>
I suppose that everybody that uses spring, uses form binding and validation. And you all defined the messages to display on validation errors. I did it with this in my config:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages" />
what will happen basically is that it will read messages.properties in my root folder of the project.
But I'd need to put messages in two separate files. Because one part of the app has to be standalone. I tried adding this just after the one above:
<bean id="messageSourceAssistenza"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename = "com.mypackage.other.assistenzamessages.properties"
/>
but it can't resolve those messages at all. How to solve this?
You should be able to use ResourceBundleMessageSource.setBasenames that accepts array of base names:
Set an array of basenames, each
following ResourceBundle conventions:
essentially, a fully-qualified
classpath location. If it doesn't
contain a package qualifier (such as
org.mypackage), it will be resolved
from the classpath root.
The associated resource bundles will
be checked sequentially when resolving
a message code. Note that message
definitions in a previous resource
bundle will override ones in a later
bundle, due to the sequential lookup.
Sample configuration as follows:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages_1</value>
<value>messages_2</value>
...
<value>messages_n</value>
</list>
</property>
</bean>
I would like to deploy multiple independent copies of a particular web-app on the same tomcat server under different context paths. Each web-app will need different configuration settings (database name, password, etc), but I would like to keep the wars exactly identical.
My plan was to have the app figure out its context path on startup, then read a specific .properties file outside of tomcat identified by the context path. For example, if a war was deployed to {tomcat path}/webapps/pineapple, then I would want to read /config/pineapple.properties
I've been trying to find a way to inject an instance of ServletContext via spring (3), but all the advice I've seen so far use the deprecated ServletContextFactoryBean.
Is there a better way to get the context path injected or better way to load external files based on the context path?
With the help of ServletContextAttributeFactoryBean and Spring EL, you can reference ServletContext init parameters (<context-param> in web.xml) like that:
#{contextAttributes.myKey}
This allows you to use PropertyPlaceHolderConfigurer and load property files from arbitrary, user-defined locations:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="#{contextParameters.APP_HOME}/conf/app.properties"/>
</bean>
The corresponding definition of the ServletContext init parameter in Tomcat's context.xml:
<Parameter name="APP_HOME" value="file:/test" override="false"/>
Or in your app's web.xml:
<context-param>
<param-name>APP_HOME</param-name>
<param-value>file:/test</param-value>
</context-param>
This should be the solution.
<bean name="envConfig" class="EnvironmentConfiguration">
<property name="locations">
<list>
<value>file:///#{servletContext.contextPath}.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
Extend Propertyplaceholderconfigurer to use DB to pick up the values. Example here
Load the actual values of the settings (database name, password etc) to the db as part of seed data
When your web-app's app ctx is being initialized, the properties are resolved from the DB
This is the approach we have been following and works great. If you can switch to Spring 3.1 then it has support for Environment Profiles which may be useful for you.