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

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>

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.

Is there a way to make a Spring bean's "request" scope work with Mule?

Is there a way to make a Spring bean's "request" scope work with Mule? According to the Spring documentation, "request" and "session" scopes are "Only valid in the context of a web-aware Spring ApplicationContext." But there should be a way to make Mule ESB web-aware, no? It definitely does not work out of the box. If I try adding the following declaration to my application, Mule won't even start up; it just hangs...
<beans xmlns="http://www.springframework.org/schema/beans">
<bean id="accountManager" class="com.ergo.AccountManager" scope="request"/>
</beans>
you can define CustomScope for request in Spring and use that for defining beans in mule as below.
<spring:bean
class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<spring:property name="scopes">
<spring:map>
<spring:entry key="request">
<spring:bean
class="org.springframework.context.support.SimpleThreadScope" />
</spring:entry>
</spring:map>
</spring:property>
</spring:bean>

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>

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

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

Spring MVC Application - How do I set a session scoped bean value

In my application I need to gather information on one screen and then display it on the next.
I have selected to store this information in a bean with a scope set as session ( it will be used in several other screens after the initial data gathering screen)
The Manager is configured as follows:
<bean name="/springapp.htm" class="foo.bar.controller.springcontroller">
<property name="sessionBeanManager" ref="sessionBeanManager" />
</bean>
The bean is configured as follows :
<bean id="sessionBean" class="foo.bar.sessionBean" scope="session">
<aop:scoped-proxy/>
<property name="beanValue" value="defaultValue" />
</bean>
<bean id="sessionBeanManager" class="foo.bar.sessionBeanManagerImpl">
<property name="sessionBean" ref="sessionBean"/>
</bean>
And I am outputting on the jsp page with
<c:out value="${sessionBean.beanValue}"></c:out>
but whenever I load the page the value is empty?
It seems to me that the bean is loading OK but is not populated with the value, which leads me to think that either the session bean is not being populated or the bean is not being created as a session bean?
You can reference spring session beans with the following syntax in your EL in the jsp.
${sessionScope['scopedTarget.messageUtil'].flashMessages}
That calls getFlashMessages() on this bean
<bean id="messageUtil" class="mypackage.MessageUtilImpl" scope="session">
<aop:scoped-proxy proxy-target-class="false"/>
<property name="messageSource" ref="messageSource" />
</bean>
Spring beans are not visible in the views (JSPs in your case) unless you add them first to the model.
You have to add your sessionBean to the model in the controller to make it available to the view.
model.addAttribute("sessionBean", sessionBean);

Resources