0

I've created a webapp using Spring MVC. I've set up internationalization through an interceptor in my dispatcher serlvet like this:

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="es" />
</bean>

Then I can access current locale in my controllers like this:

LocaleContextHolder.getLocale()

Which is awesome, because I can then change my locale at any point and access current config anywhere.

Now I would like to do the same with some custom variables that behave in a similar fashion, in the sense that they slightly affect all of my app's behavior. (e.g. an "accesible" parameter indicates the app it should offer only accesible content).

I understand I can create my own interceptor to intercept any param named "accesible" like so:

<bean id="localeChangeInterceptor" class="com.myapp.MyInterceptor">
    <property name="paramName" value="accesible" />
</bean>

But is there a way that I can access this value throughout my app like the LocaleContextHolder object does with locale? Can I make Spring inject it through a Cookie likeso?

4

1 回答 1

0

When you say throughout your app, do you mean the 'entire' app (like service, dao/repository) or just the web part of the app? Would a RequestContextHolder (http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/request/RequestContextHolder.html) help your case? It has an option to retrieve currentRequestAttributes which is an implementation of RequestAttributes interface.

I have not tried it personally, but the RequestAttributes javadoc quotes "Supports access to request-scoped attributes as well as to session-scoped attributes, with the optional notion of a "global session"."

于 2013-09-26T16:21:46.567 回答