Spring can't load my css files - css

Spring can't load my css files and this is my architecture:
Project
Webcontent
WEB-INF
pages
index.jsp
css
jquery-ui.css

You can put the css file and other files like javascript, images in the folder resources
Project
Webcontent
resources
css
jquery-ui.css
WEB-INF
pages
index.jsp
And put the following line in spring configuration
<mvc:resources mapping="/resources/**" location="/resources/" />
And give the reference in jsp pages like
<link href="${pageContext.request.contextPath}/resources/css/jquery-ui.css"
rel="stylesheet">

As fvu explained, the content inside WEB-INF is not accessible over HTTP. The Webcontent directory in
your directory structure looks like it was created for this particular purpose: storing CSS files, images and JavaScript files.
Even after you move your CSS files, they will not be accessible over HTTP automatically because Spring MVC will intercept these
HTTP requests. You need to use the <mvc:resources> tag in your bean configuration:
<mvc:resources mapping="/webcontent/**" location="/Webcontent/" />
Your CSS files will be accessible at URLs like the following:
http://localhost:8080/you-app-name/webcontent/...
There is a lot of information on <mvc:resources> at Stackoverflow.

Related

Spring MVC resource TAG in MVC configuration

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.

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/**" />

Integrating Twitter Bootstrap within a SpringMVC application

I'm starting out with building basic applications in SpringMVC. At the same time, I wanted to use some easy to setup UI frameworks like Twitter Bootstrap. But, No clue on how to set it up.
Question:
Where do I place the downloaded bootstrap folder?
What I have so far.
I would put these in src/main/resources NOT under WEB-INF. These don't need to be protected.
Also, make sure you tell Spring where they are in your dispatcher servlet config file as per the documentation.
<mvc:resources mapping="/resources/**" location="/resources/" />
If you're using Spring security as well you'll need to make sure that the resources are not protected.
<http pattern="/resources/**" security="none"/>
You don't need the .less files unless you plan to compile custom css. Maven projects, you typically place them in the resources folder. resources/assets/css and resources/assets/js
In the JSP:
<spring:url scope="page" var="bootstrapStylesheetUrl" value="/resources/assets/css/bootstrap.css"/>
<spring:url scope="page" var="bootstrapResponsiveStylesheetUrl" value="/resources/assets/css/bootstrap-responsive.css"/>
<spring:url scope="page" var="bootstrapJavascriptUrl" value="/resources/assets/js/bootstrap.js"/>
And then in the head tag
<script src="${pageScope.bootstrapJavascriptUrl}"></script>
<link rel="stylesheet" href="${pageScope.bootstrapStylesheetUrl}"/>
<link rel="stylesheet" href="${pageScope.bootstrapResponsiveStylesheetUrl}"/>
Also, don't forget to add the spring taglib to the top of your jsp
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
In your spring servlet context config(productmgmt-servlet.xml) add the line:
<mvc:resources mapping="/resources/**" location="classpath:/"/>

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" />"/>

Setup a Filter that will set the Response Header to only Image File(s)

I am having a caching problem based on the discussion that I have in this link
But I am not sure how to go about with the suggestion on setting the response headers on my Spring MVC.
Does anybody know how to setup a some sort of a filter that will add add a response header only on image files?
I currently am not an expert on J2EE web development with SPring MVC.
Any idea?
Spring comes with a Resources Servlet.
<!-- Serves static resource content from .jar files such as blartoch.jar -->
<servlet>
<servlet-name>Resources Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>50</load-on-startup>
</servlet>
<!-- Map all /resources requests to the Resource Servlet for handling -->
<servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
We are using an ear file and all of my resources are in a jar in the ear. If you are only deploying a WAR file (perhaps to tomcat), then try putting your resources in a jar and putting the jar in your WAR file's /WEB-INF/lib directory.
If you store your resources (inside the jar) in the following directory:
/META-INF/common/images
requests for your resources will look something like this:
<img src="<c:url value="/resources/common/images/cuteKitten.jpg"/>"/>

Resources