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'/>"
Related
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;">
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
Is it possible do the following by using jersey?
<mvc:resources mapping="/css/**" location="/public/css/" />
<mvc:resources mapping="/js/**" location="/public/js/" />
<mvc:resources mapping="/views/**" location="/public/views/" />
So, i need to handle /css/some.css like /public/css/some.css
Thanks!
Jersey itself doesn't serve static resources, presumably you're integrating with Spring that should work already as long as you registered the dispatch servlet and you don't have a JAX-RS resource at the top level like #Path("/"), subset them to #Path("/services") for example.
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)
in Spring 3 MVC dispather-servlet.xml with the configuration below, it seems like everytime a .js file is called the interceptor is kicked off.
<mvc:interceptors>
<bean class="com.something.SomeInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/jsp/**" location="/jsp/" />
My view/jsp calls four .js and the interceptor runs four times...
What is the proper way to set up the xml file so that this does not happen?
thanks
It's actually the browser that is requesting the JS files, so 4 HTTP requests are being made to your application. You'll need to use the "mapping" element of mvc:interceptor to select a subset of paths that the interceptor will be applied to. For example:
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors