I use interceptors in order to cache some pages that rarely updated. Works well, here is the code:
<bean class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="2592000"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
<property name="cacheMappings">
<props>
<prop key="/my/**">0</prop>
<prop key="/login">0</prop>
<prop key="/logout">0</prop>
<prop key="/change-language">0</prop>
</props>
</property>
</bean>
Basically, it says the following thing: cache all the pages except of those which are in the cacheMapping section. But I have some issue - in case of changing the language the request itself is not cached, but after actual changing I just redirect to the current page and if this page is cached (via previous settings) - user can't see the changes.. I tried to send a no-cache in the header (in the redirection request) but seems that interceptor's settings have more priority...
Any suggestions how can I solve it? Thank you in advance.
Related
I am using Spring MVC, and freemarker as my view resolver, (not that that matters) I have multiple template loaders defined like this:
<bean id="urlTemplateLoader" class="com.URLTemplateLoader">
<property name="baseUrl" value="http://xxxx:8080/ftl/"/>
</bean>
<bean id="defaultTemplateLoader" class="com.WebappTemplateLoader">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
</bean>
<bean id="multiTemplateLoader" class="freemarker.cache.MultiTemplateLoader">
<constructor-arg>
<list>
<ref bean="urlTemplateLoader"/>
<ref bean="defaultTemplateLoader"/>
</list>
</constructor-arg>
</bean>
this is designed to check a CMS server first, then use the local file. The view resolver is defined like this:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="preTemplateLoaders" ref="multiTemplateLoader"/>
<property name="freemarkerSettings">
<props>
<prop key="default_encoding">UTF-8</prop>
<prop key="localized_lookup">false</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="template_update_delay">0</prop>
</props>
</property>
</bean>
Now the problem is that if the file is NOT on the remote server when it firsts looks for it, it never checks again until the server is re-started. I set the template_update_delay to 0, so once it is there, any time I update it, it automatically pulls the new one, which is great, -- but how do I get it to check again if it is not there in the first place?
As the JavaDoc of MultiTemplateLoader says:
On every request, loaders are queried in the order of their appearance in the array of loaders provided to the constructor. However, if a request for some template name was already satisfied in the past by one of the loaders, that Loader is queried first (a soft affinity).
I will add a sticky property to it (which enables/disables "soft affinity") in 2.3.24 (expected in early March), but I can't change its default until 3.0.0 or something like that. Meanwhile, you can do that in a custom TemplateLoader (copy-paste the source code of MultiTemplateLoader and just drop the affinity part from it... should be easy).
My project makes use of Spring Tiles nad I need to implement a plain jsp approach, so the project has a page that makes use of tiles and I want to incorporate a iframe that loads html so that I can refresh as needed. I thin I found my solution to implement the resolver to load an html file.
I have a consern about resolver conflicts. Has anyone combined a number of viewresolers in their app?
You can define multiple view revolvers in your spring configuration file and set an order to them.
<bean id="viewResolverTiles"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
</property>
<property name="order" value="1" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="2"/>
</bean>
Note how we defined order property in both the view resolvers. Thus by default the Tiles based view resolver will be invoked. If Spring doesn't find view in that it moves to JSP view resolver.
Documentation: 16.5 Resolving views
I hope this helps.
I'm trying to configure Spring MVC to have the following scheme :
any url starting with "{context}/resources" should be cacheable
any other url should not be cacheable (in my case, everything by "resources" are dynamic pages )
The first part is pretty easy using mvc:resource :
<mvc:resources mapping="/resources/**" location="..." cache-period="3600"/>
I'm kinda lost for the second part.
By default, it seems like the "dynamic resources" have no cache-related info (no cache-control header, no pragma, etc...). Some browsers might cache things (FF), some might not (Chrome), which is understanable.
There are many posts about how one can use a combinason of and WebContentInterceptor to make some pages cacheable :
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/foobar/**"/>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptor>
</mvc:interceptors>
However in my case this is not useable as such, since I don't have a path expression to match (exclusion is notoriously impossible).
So is there a way I can express this :
* do what mvc:resources do for resources
* do "something" for all other pages ?
The only alternative I can see would be to write a custom Interceptor which would check whether somehing is a resource, but that sounds a bit strange not to be able to define cache properties globally.
Thanks
Spring MVC behaves just the way you want it. The WebContentInterceptor intercepts and adds cache-control only to the requests for text/html resources.
The way to do it for all urls is to leave <mvc:mapping> out of the configuration:
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptor>
Also, because <mvc:interceptors> & <mvc:resources> are independent, any particular order of those two tags is not important (unlike suggested in the answer here).
I am on a project using Spring 3.x MVC, and have implemented our controllers using annotations. We recently have a requirement to implement HandlerInterceptors, to which I have had some problems. When I specify in my configuration (dispatcher-sevlet.xml), the interceptor
<bean id="myInterceptor" class="com.myProject.controllers.MyInterceptor" />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list><ref bean="myInterceptor"/></list>
</property>
</bean>
then all is well, that is, any URL matches hits the myInterceptor code. When I try to add
<property name="mappings">
<props>
<prop key="/addFile.request">myFileController
</prop>
</props>
</property>
then I never hit the myInterceptor code...I have also tried to implement the above mapping code using #RequestMapping annotations.
It's easier to use the <mvc:interceptors> tag to configure interceptors if you're using annotation-based configuration.
E.g
<mvc:interceptors>
<!-- This runs for all mappings -->
<bean class="my.package.GlobalInterceptor"/>
<mvc:interceptor>
<!-- This one only runs for a specific URL pattern -->
<mvc:mapping path="/admin/*"/>
<bean class="my.package.AdminInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
Hi im wondering would it be possible to create global interceptor and set locale there.
I have urlrewrite rules to rewrite /fr/* to /*?siteLang=fr
I see examples how to set locale based on parameter but they all are the same and require me to use url mappings. Is it possible to do it globally so that locale interceptor gets called on each request no matter what controller is it for?
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLang"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>
/*=dispatchController
</value>
</property>
</bean>
There is no such thing as dispatchController in my xml so i cant use it but idea would be to intercept everything (in whatever way).
i would basically like to have urls with locale at the beginning of uri followed by the application bit like
/fr/user/details
/de/products/hifi
etc
different controllers using the same convention of rewriting url and never using siteLang for controller specific reasons.
Thanks
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>