Spring MVC resource TAG in MVC configuration - spring-mvc

I'm trying to find a clean solution to configure spring mvc to handle requests to static resources using the tag resources in my configuration.
It works well so far for simple configuration like:
<mvc:resources mapping="/img/**" location="file:${environment_variable}/mystaticfolder/img/" cache-period="31556926" />
<mvc:resources mapping="/css/**" location="file:${environment_variable}/mystaticfolder/css/" cache-period="31556926" />
and so on..
But i would like to be able to redirect the requests coming with a path containing "content" (always static files)+ specific_folder with some kind of regular expression (like rewrite rules).
For example:
<mvc:resources mapping="/content/${specificfolder}**"
location="file:${environment_variable}/content/${specificfolder}/" cache-period="31556926" />
Without doing a configuration for each folder under content.
Is that possible ?.
I could not find some detail about what i can put in the "location" attribute of the tag (a link could be useful too)
Thanks in advance for any tips you can share.
Regards.
S.

Related

Spring mvc does not find my /opt/ path

I'm trying to access to static resources outside context root with spring mvc (yes, it's possible), but the "file:/opt/final-test/target/**" is red and I cannot access to anything. (404).
<mvc:resources mapping="/generatedThumbnails/" location="file:/opt/final-test/target/**" />
I already tried to put :
file:/opt/final-test/target/**
file:/opt/final-test/target/
file:/opt/final-test/target
but nothing is good.
Can you help me ? Thanks
Add two stars to the mapping /generatedThumbnails/** :
<mvc:resources mapping="/generatedThumbnails/**" location="file:/opt/final-test/target/**" />

Spring mvc resources not loading on first run

I m developing a spring web application .
I have put all my resources folder in webcontent folder and configured it in my dispatcher.xml
<mvc:resources location="/asset/" mapping="/asset/**" />
I have configured my startup page as following
<mvc:view-controller path="/" view-name="Framework/start"/>
My application is running fine and all the resources are also loading but not on the first run. Means when I deploy my application on tomcat7 and hit the url for the first time the css are not loaded also my href which is mapped to a controller is also not working but once I am logged in and logout everything works fine.
After lots of effort i concluded that the problem was not with the resource path but the problem was due to the interceptor . The authentication interceptor that i have added was called multiple times due to the request to the resources and as there was no session created till that time it was returning false.
Hence i exclude any calls to resources folder from the interceptor in the following way-
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/asset/**"/>
<bean class="com.model.AuthenticationInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Also one imp thing mvc:exclude-mapping is added from spring 3.2 onwards so one need add the schema "http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"

How to configure spring mvc project which contains controller both annotated and configured

Can any one tell me, how to configure spring mvc project that contains controllers both annotated and configured??
I want to configure static pages using ParametrizedViewController and rest of all using Annotations.
But it is not working??
Please someone tell the configuration file(dispatcher-servlet.xml in my case) which will contain both this requirements.
Thanks in advance.
<!-- Registering MVC stuff necessary for #RequestMapping -->
<mvc:annotation-driven />
<!-- Your view controller definition -->
<mvc:view-controller path="/home" view-name="home" />

Spring MVC : using wildcards in <mvc:resources>

I'm implementing a cache busting system for a Spring MVC application.
For this system to work, I have to strip the "cache busting code" from a given url. Let's say my generated cache busting code is "123" and I have a .css url that is: /public-123/css/style.css. In this example, I want /public/css/style.css to be succesfully called (-123 must be stripped).
This works in my "mvc-config.xml" context file:
<mvc:resources mapping="/public-123/**" location="/public/" />
But I would also like any cache busting code to work, even if it's not the current one. For example, I would also like /public-456/css/style.css to reach the style.css file.
If I try to add another wildcard to the mapping:
<mvc:resources mapping="/public-*/**" location="/public/" />
It doesn't work! I receive a 404....
How could I specify the "mapping" attribute so any code after the "public-" part is well managed?
One way to handle this is to use Spring EL, as shown in the Spring docs:
<mvc:resources mapping="/resources-#{applicationProps['application.version']}/**" location="/public-resources/"/>
You could probably store the "123" part in a properties file so it only gets set once. E.g. via property-placeholder:
<context:property-placeholder location="classpath:myApp.properties"/>
<mvc:resources mapping="/resources-${cache.code}/**" location="/public-resources/"/>
This has the advantage of being able to read this code in your JSP pages (to generate links) via the same properties value.
I managed to get this working by manually defining the ResourceHttpRequestHandler to handle assets that are located on the filesystem alongside the <mvc:resources /> tag:
<bean id="assetsResourceHandler" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
<property name="locations">
<list>
<bean class="org.springframework.core.io.UrlResource">
<constructor-arg value="file:#{applicationProps['assets.basedir']}"></constructor-arg>
</bean>
</list>
</property>
</bean>
I guess you're doing this to achieve cache busting for your static resources.
In the meantime, Spring 4.1 has dedicated features for this, so you can remove a lot of that custom configuration.
Something like this:
<mvc:resources mapping="/public/**" location="/public/"/>
<mvc:resource-chain resource-cache="true">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>

Spring 3 doesn't return css

I just started with Spring 3 MVC today. Running into a dilemma...
web.xml maps everything ("/") to Spring. But as a result, when I put something like:
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/navigation.css" />
It is not returned by the container...
Perhaps someone could suggest how to handle this?
Thanks.
Use mvc:resources, as explained in the documentation. This allows service static resources from the web app, but also from the classpath.
How are you trying to serve it? If you are trying to serve it from the webapp itself (ie WEB-INF/static/css) You would need to include a servlet to do that for you. In the spring context you can include something like
<mvc:resources mapping="/resources/**" location="/resources/" />
You can see more here
How to handle static content in Spring MVC?
As suggested by others, use mvc:resource to serve your static resources.
<mvc:resources mapping="/resources/**" location="/resources/" />
It is also recommended to avoid using scriptlets in your JSP code if possible. You should instead use JSTL to build the correct path to your CSS file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
...
<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/navigation.css" />"/>

Resources