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)
Related
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
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
I am trying to integrate Spring MVC with my existing application. Existing application has context root as PORTAL here is my Spring MVC setting in web.xml is
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Everything works fine and I am able to access pages through Spring MVC. Pages get data from Controller but pages doesn't load the css, img and js files.
Application Folder structure is
src
webapps
-css
-img
-js
-WEB-INF
--jsp
JSP
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="css/poc.css"/>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
Error
No mapping found for HTTP request
with URI [/PORTAL/css/bootstrap.css] in DispatcherServlet with name 'spring'
As Spring MVC works on URL Pattern and every request for Image and Resources came from client is separate request, So Spring is finding the matching pattern for "/bootstrap.css" to identify is there anything to serve against this Pattern.
And there is no resource mapped against this that is why you are getting this error.
You can say Spring that these are my resources and don't go for any mapping finding, if request comes with mentioned URL.
put below line in spring-servlet.xml file.
<mvc:resources mapping="/PORTAL/css/" location="/PORTAL/css/" cache-period="0" />
this will help you to get rid of css loading errors. but problem for js and image still be there,
So try to keep all resources in one resource folder and give mapping like one below,
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="0" />
Also, import this xsd above in xml file,
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
hope this helps.
You are missing the ContextPath while refering the js and css files.
Prefix the resource references as mention below
<script src="${pageContext.request.contextPath}js/jquery.js"></script>
For me the problem is that you map the SpringDispatcherServlet to everything ( <url-pattern>/</url-pattern> ). You could map it to only one type of resource and then let the server serves the other types. Example :
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Then all you css, images, videos, applet jars etc will be served directly by the server while urls finishing with .htm will be served by Spring MVC.
If you need, you can also define multiple servlet mapping for the Dispatcher servlet.
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/>
I'm trying to map a request to static resources in a spring environment. My app server is Jetty.
In web.xml, I'm mapping various url patterns to my spring servlet:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/otherpath/*</url-pattern>
</servlet-mapping>
[many more mappings...]
Note that "/" is not mapped to my spring servlet.
In spring-servlet.xml, I'm using the mvc:resources tag to map a url to a directory with my static content:
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
This does not work as I expected. Instead of mapping
/static/ to /WEB-INF/static/,
it maps
/static/static/ to /WEB-INF/static
The reason is that the mapping given in "mvc:resources" seems to not be relative to / but relative to the path that maps to the spring servlet.
Is there a way to consider the full path, relative to / for the mapping, not the path relative to the servlet mapping?
The solution is to not use the mvc:resources tag, but to configure the corresponding handler with a bean and a URLHandlerMapping:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<props>
<prop key="/static/*">staticResources</prop>
</props>
</property>
</bean>
<bean id="staticResources" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
<property name="locations">
<list>
<value>/WEB-INF/static/</value>
</list>
</property>
</bean>
The SimpleUrlHandlerMapping with its alwaysUseFullPath property does allow more fine-grained control over the mapping.
To answer your question with one short word. No, at least I don't think so.
The servlet looks inside it's own "space" and that is after the servlet-mapping done in web.xml. That in turn is after the mapping done i your container (like tomcat)
Would it be possible to add just one servlet to / and then add two <mvc:resource />? One with /static/** and one with /otherpath/** (or whatever you need there). If not I would go with JB Nizet's solution to add two different servlets entirely.
Or you could use <mvc:default-servlet-handler/> and <spring:url>. It worked for me.
mvc:resources doesn't seem to work when application is not started on the ROOT context.
Here's the configuration that I used (note the commented bit that was doing the resource mapping to the application started under 'localhost:8080/myapp' context, though context name shouldn't be in the spring config):
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<!--<mvc:resources location="/styles" mapping="/myapp/styles/**"/>-->
<!--<mvc:resources location="/js" mapping="/myapp/js/**"/>-->
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource
requests to the container's default Servlet -->
<mvc:default-servlet-handler/>
The trick is to use spring:url to resolve your application context. Here is what I used for that:
<spring:url value="/styles/site.css" var="site_style"/>
<link rel="stylesheet" href="${site_style}" type="text/css" media="screen"/>
I'm basically using relative paths to my root app folder, while spring takes care of adding the /myapp in front of it.
It's still pretty strange that mvc:resources doesn't do that on it's own, but at least this works and its still pretty neat.