Spring 3 mvc:resources causing mvc:interceptors to run multiple times - spring

in Spring 3 MVC dispather-servlet.xml with the configuration below, it seems like everytime a .js file is called the interceptor is kicked off.
<mvc:interceptors>
<bean class="com.something.SomeInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/jsp/**" location="/jsp/" />
My view/jsp calls four .js and the interceptor runs four times...
What is the proper way to set up the xml file so that this does not happen?
thanks

It's actually the browser that is requesting the JS files, so 4 HTTP requests are being made to your application. You'll need to use the "mapping" element of mvc:interceptor to select a subset of paths that the interceptor will be applied to. For example:
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors

Related

exclude pattern with spring interceptor

I am using a spring interceptor and I want to exclude only the first page in my application.
The first page serves to authenticate then redirects towards other pages.
so I wrote in my config file
<mvc:interceptors>
<mvc:interceptor>
<mvc:exclude-mapping path="/" />
<bean class="com.app.interceptor.AuthentificationInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
But I got an error in the deployement.
Where did I go wrong ?
I am sorry, I made a mistake.
I made a confusion between the path that I saw in the URL (the first page in the URL was located at the root of the app, hence the "/"), and the path of my restangular call in the first page, which was "/contexte".
So I wrote
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/contexte/**" />
<bean class="fr.smabtp.ig.saisines.metier.interceptor.AuthentificationInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
and it works fine.

Moving mvc interceptor from module-context to web-application context

Can we move mvc interceptors from individual context file to web-application context.xml
Thus removing interceptor from individual module context?
You can put them in the web-context as follow:
<mvc:interceptors>
<bean class="com.package.TheInterceptor" />
<bean class="com.package.AnotherInterceptor" />
</mvc:interceptors>

Required Some Best practices to detect the locale of a user using jsf2 + spring web flow

I am just trying to figure out some good approches to detect the locale.
The approach I am following is :
1-One language Bean with the below code
<code>
if(!classUtil.isNullObject(FacesContext.getCurrentInstance())){
return FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}
return selectedLanguage;//Return default language in case its not possible to detect the language
</code>
2- creating bean definition inside parent-flow
<code><var name="languageBean" class="com.decitysearch.classified.LanguageBean"/></code>
3- using the same in the xhtml
<code>
<f:view locale="#{languageBean.findDefaultLocale}">
<f:loadBundle var="messageResource" basename="MessageResource_en"/>
</code>
My questions here it goes like:
1-can't we make the bean entry inside spring-context rather than flow bean as I tried with spring-context but getting some scope exceptions.
2-Is there any good approaches to detect the locale
your thought process is highly appreciated.
If you are using Spring, declare these beans and call URL with parameter "locale=xx" to change locale:
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="es" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

about spring social xml configuration

i wonder "#{request.userPrincipal.name}" in configuration blow. when I run my spring social project it always has error at "#{request.userPrincipal.name}", if I set a value such as "123" my project runs well. what's wrong and is there any other configuration instead of "#{request.userPrincipal.name}" ?
<bean id="connectionRepository" factory-method="createConnectionRepository" factory-bean="usersConnectionRepository" scope="request">
<constructor-arg value="#{request.userPrincipal.name}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
This is a Spring EL expression, which means getting the user identity from the Http request. Once you apply your own User Management component, you can replace #{request.userPrincipal.name} with your own way.

setting no cache for different parts of spring mvc 3 using WebContentInterceptor?

Hi there I have developed a dynamic web application that uses Ajax to fetch data from databases and keep the GUI up to date but while testing it with IE8 I am experiencing caching issues.
I used the following code in my webmvc-config.xml file to stop the browser from caching:
<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>
and it works exactly as it should, but the problem is that now the browser obviously doesn't cache anything. what I want to know is how to modify that xml code so that it applies to the Ajax parts of the web app (which are controlled using 5 Controller files); so that the icons..etc are still cached? The path to these controller files would be something like "/admin/**"
I know that the Spring WebContentInterceptor has properties such as "setCacheMappings" and "setPathMatcher" but there is nowhere online that I can find examples of these being using in the xml config file.
ANY help would be much appreciated, it's really doing my head in.. Thanks. Jake
In your <mvc:interceptors> you can restrict the URL path each interceptor should apply to, as follows:
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/admin/*"/>
<bean id="webContentInterceptor" ..... />
</mvc:interceptor>
<mvc:interceptors>
It's all explained here.

Resources