Spring MVC Annotations and HandleInterceptors - spring-mvc

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>

Related

Freemarker not loading remote template

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).

SpringMVC serves view without controller

I've a jspx file (for example test.jspx) in folder WEB-INF/templates and i want to access it by javascript.
I haven't a controller that returns that view, and in my application I'm using TilesViewResolver.
In test.jspx i want to use spring taglib to access message-bundle.
How i can configure spring to serve test.jspx ?
I'm using:
<mvc:view-controller path="/templates/test.html" view-name="test" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/templates/</value>
</property>
<property name="suffix">
<value>.jspx</value>
</property>
<property name="order" value="1" />
</bean>
It doesn't works.
I don't know if it's possible without any controller, but you can use a controller already provided by Spring MVC: ParameterizableViewController. Just provide the name of your jsp as the viewName property.

Supporting UrlBasedViewResolver and TilesConfigurer and InternalResourceViewResolver

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.

Spring MVC resource bundles

Is it possible to have multiple resource bundles in spring mvc?
I want to separate my resource bundles for example one for errors, another for global messages, other for image names etc. so I don't have just one very big file
I'm using this
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
But here i can just specify 1 resource, can I use more?
You can just pass in a list using the setBasenames() method like this:
<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>images</value>
<value>errors</value>
<value>globalMsgs</value>
</list>
</property>
</bean>
</beans>

spring 3 mvc intercept all requests

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>

Resources