MVC resources doesn't import all files - spring

In my Dispatcher-servlet i have the mapping for resources with the tag <mvc:resources>` from spring mvc:
<!-- the mvc resources tag does the magic -->
<mvc:resources mapping="/resources/**" location="/resources/" />
In my resources folder I have some css, js, fonts, icons and other resources.
Everything works fine, but when i add some new css files to the resources/css folder it doesn't import it to my pages :(
example :
Dispatcher Servlet
<!-- the mvc resources tag does the magic -->
<mvc:resources mapping="/resources/**" location="/resources/" />
the resources folder
webapp/resources/css/
bootstrap.css(working)
bootstrap.min.css(working)
Jquery-Datatables/cssfiles.....(not working)

i have found a solution to the problem
in fact the mcv:ressources doesn't import ressources, it just create a mapping for it
so if we request /resources/css/bootstrap.css from any page anywhere in the web app , it will be redirected to the ressources folder.
the problem i had was with a framework called sitemesh which works like the asp.net Master Page
it's defines a design pattern and import files to all pages specified ...
it was my misunderstanding, thank you

Related

I cant load image in my spring mvc project

I am using spring tool suite. I have placed the image file in folder WebContent/WEB-INF/resources
<img src="WebContent/WEB-INF/resources/team_pic1.jpg" alt="Mountain View" style="width:304px;height:228px;">
This is my servlet.xml code
<mvc:resources mapping="/resources/" location="/resources/" />
I am getting error as
Failed to load resource: the server responded with a status of 404 (Not Found) at http://localhost:8080/SpringMVCTest/WebContent/WEB-INF/resources/team_pic1.jpg
Here is my directory structure
PLS let me know where I am going wrong
here is the error I get when I run through browser
Just try:
<c:url var="imgUrl" value="/resources/team_pic1.jpg" />
<img src="${imgUrl}" alt="Mountain View" style="width:304px;height:228px;">
this build the right URL to hit the resource handler.
You also have to correct the resource mapping:
if you want to deliver "subresources" then you need to add /**.
Depending on where your resource files are located, you also need to correct the location attribute: I expect that /WEB-INF/resources/ is the folder where your resources exist in the war!
So I think this it the resource configuration you need:
<resources mapping="/resources/**" location="/WEB-INF/resources/" />
Content is (usually) served from WEB-INF, so it is not part of the path.
Your configuration defines the mapping
<mvc:resources mapping="/resources/" location="/resources/" />
which says nothing more, that static resources are served from /resources, which means, they are delivered as is.
You could use ${pageContext.request.contextPath}- explanation here
<img src="${pageContext.request.contextPath}/resources/team_pic1.jpg" alt="Mountain View" style="width:304px;height:228px;">

Absolute Spring resource reference from a jsp page

I would like to know how to reference a spring resource from a JSP page without either a relative reference, such as ../.., or some cluge to get the context root.
I have this defined:
<mvc:resources mapping="/r/**" location="/resources/" />
In my jsp I would like something like <img src="r/images/image.jpg"/> or
<img src="/r/images/image.jpg"/>
What's the proper way to do this?
Do the following
<spring:url value="/r/images/image.jpg" var="imageUrl" htmlEncoding="true"/>
<img src="${imageUrl}"/>
Note that spring:url is from the Spring Tag Library

Spring MVC Static Resource Mapping

I have the following servlet mapping present -
<!-- Mapping Static Resources -->
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
My image link in the html is "/images/folder/imageName.jpg" - These images get me a 404 whereas if the change the link to "/images/imageName.jpg" and move the image to directly under the images folder it gets me the image.
Do I need to modify my servlet mapping in any way to take into account the hierarchical structure?
You need to modify links to the images. When you write
<mvc:resources mapping="/images/**" location="/resources/images/" />
Then your HTTP requests to /resources/images are translated to webapp/images folder on the server. So in the html you should have something like this:
<img src="<spring:url value='/resources/images/logo.png'/>"

How to access the images from jsp?

I am using Spring MVC 3.0.
I have to load the images which exist in the folder "images" parallel to the WEB-INFdirectory.
I have the jsp files in WEB-INF/jsp folder.
The folder structure is:
-app
--images
--WEB-INF
---jsp
---classes
...
In web.xml the url mapping for DispatcherServlet is some thing like
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Now in JSP if I load the jsp in the following way:
<img src="<%=request.getContextPath()%>/images/calogo.jpg" />
Its now working as DispatcherServlet is intercepting it I guess.
Yes you are right. Your dispatcher is intercepting your request to display the images.
According to me when you try to access the image in your jsp file it will give you 404 error.
You need to include the following line of code in your servlet.xml file.
<mvc:resources location="/images/" mapping="/images/**" />
And then everything will work.
Hope this helps you.
Cheers.
If you map / to DispatcherServlet, be sure to enable default servlet handler in Spring's config:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- add this to make Spring properly handle resources (e.g., images) -->
<mvc:default-servlet-handler />
Also, do not use <%... syntax use JSTL:
<img src="<c:url value="/images/calogo.jpg" />" />
(or better - do not use JSP at all, use e.g. ThymeLeaf)

Static resources in Spring MVC app

In my Spring mvc application I want to serve static resources using mvc:resources.
My web.xml mapping looks that:
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Where main is dispatcher servlet to serve all the content
In my servlet.xml file I added:
<mvc:resources mapping="/static/**" location="/static/"/>
and it works properly when my application context is empty (like localhost:8080/), but when I deploy application in another context it doesn't work, I got 404.
I tried many combinations:
"static/**"
"*/static/**"
Nothing works.
I'm sure it's server context problem, but I have not idea (I couldn't find the solution in Google too) how to solve this. Please, help.
I was able to succesfully map my static resources using the following convention:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
The easiest way for we that worked, was adding in the servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation) the following:
<mvc:default-servlet-handler/>

Resources