Spring WebContentInterceptor is not adding cacheSeconds to header - spring

I am trying to add configuration in spring mvc so that my static contents like js, images will be cached by browser. I have add the following in dispatcher-servlet.xml
<beans>
.............
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="31556926"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptors>
</beans>
But i still dont see the caching is enabled. I see the following in the browser debugger where it says Cache-Control:"no-cache".
Please help !!!

You can use mvc:resources for caching static files.
<mvc:resources mapping="/static/**" location="/public-resources/"
cache-period="31556926"/>
<mvc:annotation-driven/>

You're missing the cacheMappings property to configure path patterns / cache directives.
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="31556926"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
<property name="cacheMappings">
<props>
<prop key="/static/**">2592000</prop>
</props>
</property>
</bean>
But indeed, Burak Keceli's answer is spot on; you'd better use WebMvcConfigurerAdapter.addResourceHandlers or the XML configuration version with <mvc:resources/>.

I had the same problem. I added the interceptor but noting changed in the HTTP reponses.
In my case there was another spring context XML in my project where another <mvc:interceptors> was registered. It effectivly replaced my WebContentInterceptor configuration. Combining them solved it.
Perhaps something similar is going on in your configuration Abdus Samad...

Related

Configuring auto_escape in freemarker spring-mvc Application

I am using spring-mvc with freemarker-2.3.27-incubating.jar.
And bean configuration for view-resolver like below...
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/"/>
<property name="freemarkerSettings">
<props>
<prop key="template_exception_handler">rethrow</prop>
<prop key="number_format">0.########</prop>
<prop key="date_format">dd/MM/yyyy</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
Where and how can I use <#ftl output_format="HTML"> or <#ftl output_format="HTML" auto_esc=true> to enable HTML auto-scape ?
The #ftl tags you just add at the beginning of the template files themselves. But I would recommend setting recognize_standard_file_extensions to true in the freemarkerSettings properties, and then use .ftlh file extension instead of .ftl. Or, even better, you set incompatible_improvements to 2.3.27 there instead, which enables recognize_standard_file_extensions and some fixes. (Last not least, in case you want this for .ftl for some reason, you can set output_format to HTMLOutputFormat in freemarkerSettings properties too.)

How do I automatically reload my properties in my Spring XML appilcation context?

I’m using Spring 3.2.11.RELEASE. I currently have the following set up in my application context file for the purposes of loading a cron trigger based off a schedule defined in a properties file (the property = cron.schedule) …
<bean id="localPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
</bean>
…
<bean id="updateResourcesJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myService" />
<property name="targetMethod" value="myMethod" />
<property name="concurrent" value="true" />
</bean>
<bean id="updateResourcesCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="myJob" />
<property name="cronExpression" value="${cron.schedule}" />
</bean>
My question is, I would like to create an XML configuration in my context file that allows me to edit my properties file and have everything automatically reloaded without having to restart my server or re-deploy my application. I have read several places about Apache Commons Configuration, but I can’t figure out how to take the above and rewrite an XML config that would utilize the configuration.
Thanks for any help, - Dave

Spring multiple PropertyPlaceholderConfigurer (in multiple projects) how to override an already defined property?

I have a situation where I need to override properties from one project in another. In project 1, where I have no control over the source or the configuration, there is the following config:
<bean id="propSource1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/sample-properties/prop1.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="-10"/>
</bean>
In another project, which I am working on, I have following config:
<bean id="propSource2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/sample-properties/prop2.properties</value>
<value>classpath:/sample-properties/prop3.properties</value>
</list>
</property>
<property name="order" value="1000"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Individually both configurations work fine on their own. The problem happens when I have some property in prop2.properties or prop3.properties that I want to use to override the value for the same property from prop1.properties. It always uses the value in prop1.properties. I have researched quite a bit but did not find anything useful.
Any suggestion is appreciated.
The answer is in #M.Deninum's comment.

Internationalisation with Spring

I have an existing Spring/GWT Application which i need to add internationalisation to. My understanding is that i can use Spring's "ResourceBundleMessageSource" to automatically select the appropriate messages_* file depending on the users location. I tried following this tutorial but i can't seem to get the Application to display my strings in French. As it stands, I've added 2 files messages_en_US.properties and messages_fr_FR.properties into my src/main/resources/i18n folder and added the following to the applicationContext.XML:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>classpath:i18n/messages</value>
</property>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Just wondering 1) if i need additional configuration/glue code and 2) if i can test this easily without having to set the Language/Locale to French on my Redhat Server?
It's likely that your browser sends just "fr" language tag in Accept-Header. Spring is notorious for problems with fall-back, so you may need to copy the messages_fr_FR.properties as messages_fr.properties.
I am sure there must be some ways to configure fall-back, so you want have to use messages_en.properties (try your application with other English locales...), just messages.properties should do, but I am just too lazy/tired to look for solution at the moment.
Here you need to specify below bean in spring.xml.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
This works perfectly fine when you pass lang=es in query string. If still any issue remain.you can check the working example Here .

Loading .properties file in a jar from my web-app

I have created a JAR that I need to use in my WEB-APP. Both are created with spring framework. I would like to load a .properties file outside the JAR file, in the main context of the web-application. And I want to do it with the facilities that Spring offers us.
I've tried to do something like this in my spring.xml file inside the JAR:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/my.properties</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="myJob" />
<property name="cronExpression" value="${my.cronExpression}"/>
</bean>
</property>
</bean>
Trying to load my.cronExpression from my.properties file. But without any success.
I always get this error:
Could not resolve placeholder 'my.cronExpression'.
I've tried to change the location with many variants, using classpath:/WEB-INF/classes/my.properties etc...
But I'm not able to load the configuration file.
Thanks for your help.
Use classpath:my.properties - /WEB-INF/classes is root of your classpath.
Try declaring it as follows:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/my.properties</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>
I have gone through your code and want you to try this code snippet
It works well for me :)
<bean id="placeholderProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/WEB-INF/classes/my.properties" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />
</bean>

Resources