how to set header no cache in spring mvc 3 by annotation? not is
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
There is no such option. You can use an interceptor:
<mvc:annotation-driven/>
<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:interceptors>
(taken from here)
On one hand it is logical not to have such annotation. Annotations on spring-mvc methods are primarily to let the container decide which method to invoke (limiting it by a request header, request url, or method). Controlling the response does not fall into this category.
On the other hand - yes, it will be handy to have these, because when controllers are unit-tested it is not relevant to test http header stuff (or is it?). And there are #ResponseBody and #ResponseStatus, which do specify some response properties.
To override the settings for certain controller mappings use the cacheMappings properties object on the WebContentInterceptor
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="2100" />
<property name="useExpiresHeader" value="true" />
<property name="useCacheControlHeader" value="true" />
<property name="useCacheControlNoStore" value="true" />
<property name="cacheMappings">
<props>
<prop key="/myUncachedController">0</prop>
</props>
</property>
I know this is old but this might be helpful to some.
If you wanted to add a lot more logic to when you cache and when you don't you can also write a custom interceptor.
For example if you wanted to disable caching in the response only when the browser is IE or only from specific urls you can do that as well by extending the HandlerInterceptor interface.
By doing that you can have a lot of control over exactly what happens. It's not as easy as just setting the header for everything at once or just typing in the changes to the response in each controller but it is also not that hard and is a better long term solution in my opinion. It is also a good thing to know how to do in spring generally.
This is a pretty good tutorial for it:
http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/
Related
We are using primefaces media component and it generates the url as /javax.faces.resource/dynamiccontent.properties;/ .pdf which contains semicolon(;).
Due to that, we are getting exception i.e. The request was rejected because the URL contained a potentially malicious String.
In Spring Security 5 update by default StrictHttpFirewall is enabled.
We can specify to allow semicolon by using setAllowSemicolon(true) in StrictHttpFirewall.
But this will be applicable for all URL.
Is there any way through which we can configure to allow semicolon only for specific URL?
As the answer above indicated I also added the following XML definition for a custom firewall that allows semi-colons.
<bean id="myHttpFirewall" class="org.springframework.security.web.firewall.StrictHttpFirewall">
<property name="allowSemicolon" value="true"/>
</bean>
<security:http-firewall ref="myHttpFirewall"/>
However byt itself this had no affect. To use that firewall in my application I had to add it to my FilterChainProxy as follows:
<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
<constructor-arg>
<list>
<security:filter-chain pattern="/**" filters="...."/>
</list>
</constructor-arg>
<property name="firewall" ref="myHttpFirewall"/>
</bean>
If you use xml configuration, Declare your bean:
<bean id="customStrictHttpFirewall"
class="org.springframework.security.web.firewall.StrictHttpFirewall">
<property name="allowSemicolon" value="true"/>
</bean>
then in security.xml ref:
<http-firewall ref="customStrictHttpFirewall"/>
if you use annotations, You can search for answers, like this!
I am creating multiple bean instances of a class with different property values in spring configuration.
For example:
<bean id="myBeanA" class="MyClass">
<property name="identifier" value="A"/>
</bean>
<bean id="myBeanY" class="MyClass">
<property name="identifier" value="B"/>
</bean>
I am new to Spring framework and I am just curious to know the pros and cons to doing this.
of course you can (as you already show - ignoring typo). Pros/Cons? None, you just do it if it make sense for you...
<bean id="primaryService" class="MyService">
<property name="url" value="http://domain1/"/>
</bean>
<bean id="premiumService" class="MyService">
<property name="url" value="http://domain2/"/>
</bean>
E.g. you can have two instances of the same service type, but accessing different remote URL. You can use premiumService only for paying customers as kind of quality of service (e.g. domain2 is stronger machine)
In my REST API I want to allow the user to set the locale using a lang parameter, i.e.
http://somehost/resource?param1=value1&lang=fr
If the lang parameter is not present in the URL then the Accept-Language header should be used and set as the Locale.
I'm using Spring's i18n features in my REST API. I have looked through the documentation and configured the necessary beans. If I send a request with the Accept-Language header it seems to work OK, when I call LocaleContextHolder.getLocale() it returns the locale I set in my header.
If I use the lang URL parameter it does not work.
How can I configure Spring to use the locale parameter too?
<bean id="localeInterceptor"
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="localeInterceptor" />
</list>
</property>
</bean>
<bean id="sessionLocaleResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<bean id="messages" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
I have two suggestions to try out. First, are you certain that the LocaleChangeInterceptor is getting invoked? Most of the configurations I've seen have an id of handlerMapping for the HandlerMapping. The other suggestion is concerning another id, namely sessionLocaleResolver, I think it should be localeResolver. I'm not sure if Spring relies on those id values or the class types by default for wiring all of this together, but it is worth a shot.
About FreeMarker, is it possible to instruct it to treat all numbers as "computer" ones by default?
I tried to apply
cfg.setSetting(Configurable.NUMBER_FORMAT_KEY, "computer");
or
cfg.setNumberFormat("computer");
to configuration object, but the outcome is not the desired. Believing this is the documentation's way to do it, is there anything wrong?
The number format you specify must be something that java.text.DecimalFormat supports. The closest thing you can do right now is switching the locale to en_US (and better ensure that it doesn't use groping; see http://freemarker.org/docs/app_faq.html#faq_number_grouping). Or, of course, you can write ?c-s all over, but I suppose that's what you wanted to avoid.
Try with:
cfg.setNumberFormat("#");
It worked for me
Based on docs it is possible to set it on Freemarker Configuration object.
Typical functional spring context initialization:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths">
<list>
<value>classpath:META-INF/templates</value>
</list>
</property>
<property name="freemarkerSettings">
<props>
<prop key="number_format">computer</prop>
</props>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="cache" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="requestContextAttribute" value="request" />
</bean>
Notice that number_format (or numberFormat for newer version of freemarker) is set to FreeMarkerConfigurer.
To your question... My guess is you set it to wrong config object or scope.
It is worked in the latest version, I can do this in my Spring boot application
freeMarkerConfigurer.getConfiguration().setNumberFormat("computer");
I have a Spring 2.5.x application which I'm migrating to Spring 3 and just bumped into a little problem.
I have an handler mapping like so:
<bean id="handlerMappings1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="interceptor1" />
<ref bean="interceptor2" />
....
<ref bean="interceptorN" />
</list>
</property>
<property name="urlMap">
<map>
<entry key="/url1.html" value-ref="controller1" />
<entry key="/url2.html" value-ref="controller2" />
....
<entry key="/url100.html" value-ref="controller100" />
</map>
</property>
</bean>
and another one like this:
<bean id="handlerMappings2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/urlA.html" value-ref="controllerA" />
<entry key="/urlB.html" value-ref="controllerB" />
....
<entry key="/urlN.html" value-ref="controllerN" />
</map>
</property>
</bean>
I'm slowly replacing both with #RequestMapping annotations with a <context:component-scan> (which basically registers a DefaultAnnotationHandlerMapping).
In Spring 3 I saw the <mvc:interceptors> tag which can be used to add interceptors to certain URLs but you can specify only one interceptor, at least that's what I see from the schema.
From what I can figure, I have to register one of these for each interceptor which will duplicate all my URLs for as many times as I have interceptors (and I don't even know in what order they will run).
On the other hand I can't add the iterceptors on the DefaultAnnotationHandlerMapping because they will run for all my controllers annotated with #RequestMapping and I don't want that.
So how can I specify interceptors is Spring 3 for some URLs, without repeating the URL's and
keeping the URL to controller mapping based on the #RequestMapping annotation?
You could have a look at the SelectedAnnotationHandlerMapping and the IgnoreSelectedAnnotationHandlerMapping classes from the springplugins project. The sources are some years old but the idea still stands.
There is a presentation on the creator's blog here: Spring Framework Annotation-based Controller Interceptor Configuration. Make sure you also read the comments to the blog post.
One option would be to create a custom interceptor which can delegate to a collection of injected interceptors.