Spring MVC 3 and handling static content - am I missing something?

This problem is solved in spring 3.0.4. RELEASE where you can use mvc:resources mapping="..." location configuration element in your spring dispatcher configuration file.

This problem is solved in spring 3.0.4. RELEASE where you can use configuration element in your spring dispatcher configuration file. Check Spring Documentation.

Since I spent a lot of time on this issue, I thought I'd share my solution. Since spring 3.0.4, there is a configuration parameter that is called (more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site's root. In order to use this, use a directory structure that looks like the following: src/ springmvc/ web/ MyController.

Java WebContent/ resources/ img/ image. Jpg WEB-INF/ jsp/ index. Jsp web.

Xml springmvc-servlet. Xml The contents of the files should look like: src/springmvc/web/MyController. Java: package springmvc.

Web; import org.springframework.stereotype. Controller; import org.springframework.web.bind.annotation. RequestMapping; @Controller public class WorldController { @RequestMapping(value="/") public String index() { return "index"; } } WebContent/WEB-INF/web.

Xml: springmvc org.springframework.web.servlet. DispatcherServlet 1 springmvc / WebContent/WEB-INF/springmvc-servlet. Xml: WebContent/jsp/index.

Jsp: Page with image " /> Hope this helps :-).

Thank you very much. – Alfredo O Jun 30 at 22:16.

I found a way around it using tuckey's urlrewritefilter. Please feel free to give a better answer if you have one! In web.

Xml: UrlRewriteFilter org.tuckey.web.filters.urlrewrite. UrlRewriteFilter UrlRewriteFilter /* app org.springframework.web.servlet. DispatcherServlet app /app/* In urlrewrite.

Xml: / /app/ ^(^\. +)$ /app/$1 /app/** /$1 This means that any uri with a '. ' in it (like style.

Css for example) won't be re-written.

I've just been grappling with this issue in Spring MVC 3.0 and I initially went with the UrlRewriteFilter option. However I was not happy with this solution as it "didn't feel right" (I'm not the only one - see the link above to the Spring Forums where the word "hack" appears a few times). So I came up with a similar solution to "Unknown (Google)" above but borrowed the idea of having all static content served from /static/ (taken from the Spring Roo version of the Pet Store app).

The "default" servlet did not work for me but the Spring Webflow ResourceServlet did (also taken from Spring Roo generated app). Web. Xml: mainDispatcher org.springframework.web.servlet.

DispatcherServlet 2 Resource Servlet org.springframework.js.resource. ResourceServlet 1 mainDispatcher / Resource Servlet /static/* The only change I made to JSPs was to add the /static/ path to URLs for CSS, JS and images. E.g."${pageContext.request.

ContextPath}/static/css/screen. Css". For Maven users the dependency for "org.springframework.js.resource.

ResourceServlet" is: org.springframework. Webflow org.springframework. Js 2.0.8.RELEASE.

Not a bad solution nickdos- thank you! I still just don't "get it" as to why there isn't a resource servlet in core spring mvc (rather than having to add another dependency with web flow) or some other solution out of the box. Urlrewrite works fine for me so I'll stick with that for the time being!

Cheers, Hamo – hamo Jan 13 '10 at 11:33 Looking back over the standard (non-Roo) version of the Spring Pet Clinic app, I noticed that the servlet definition for "default" is commented-out with the additional comment: "Uncomment this in containers (GlassFish) that do not declare this implicit definition out of the box". The explicit package declaration for default is org.apache.catalina.servlets.DefaultServlet. So this may be your "out of the box" resource servlet(?).

I use Jetty for dev work and it seems Jetty does not provide an implicit default servlet (like Glassfish). – nickdos Jan 14 '10 at 21:45.

In Spring 3.0. X add the following to your servlet-config. Xml (the file that is configured in web.

Xml as the contextConfigLocation. You need to add the mvc namespace as well but just google for that if you don't know how! ;) That works for me Regards Ayub Malik.

This also worked. I think it's less intrusive. – Alfredo O Jun 30 at 22:22.

If I understand your issue correctly, I think I have found a solution to your problem: I had the same issue where raw output was shown with no css styles, javascripts or jquery files found. I just added mappings to the "default" servlet. The following was added to the web.

Xml file: default *. Css default *. Js This should filter out the javascript and css file requests from the DispatcherRequest object.

Again, not sure if this is what you are after, but it worked for me. I think "default" is the name of the default servlet within JBoss. Not too sure what it is for other servers.

1 I actually don't want to use the default servlet- that couples me to jboss/tomcat – hamo Jan 6 '10 at 8:41.

My way of solving this problem is placing all your actions with a specific prefix like "web" or "service" and configure that all url's with that prefix will be intercepted by the DispatcherServlet.

The URLRewrite is sort of a "hack" if you want to call it that. What it comes down to is, you're re-inventing the wheel; as there are already existing solutions. Another thing to remember is Http Server = Static content & App server = dynamic content (this is how they were designed).

By delegating the appropriate responsibilities to each server you maximize efficiency... but now-a-days this is probably only a concern in a performance critical environments and something like Tomcat would most likely work well in both roles most of the time; but it is still something to keep in mind none the less.

I solved it this way: Spring MVC Dispatcher Servlet / default *. Jpg default *. Png default *.

Gif default *. Js default *. Css This works on Tomcat and ofcourse Jboss.

However in the end I decided to use the solution Spring provides (as mentioned by rozky) which is far more portable.

I know there are a few configurations to use the static contents, but my solution is that I just create a bulk web-application folder within your tomcat. This "bulk webapp" is only serving all the static-contents without serving apps. This is pain-free and easy solution for serving static contents to your actual spring webapp.

For example, I'm using two webapp folders on my tomcat. Springapp: it is running only spring web application without static-contents like imgs, js, or css. (dedicated for spring apps.) resources: it is serving only the static contents without JSP, servlet, or any sort of java web application.

(dedicated for static-contents) If I want to use javascript, I simply add the URI for my javascript file. EX> /resources/path/to/js/myjavascript. Js For static images, I'm using the same method.

EX> /resources/path/to/img/myimg. Jpg Last, I put "security-constraint" on my tomcat to block the access to actual directory. I put "nobody" user-roll to the constraint so that the page generates "403 forbidden error" when people tried to access the static-contents path.

So far it works very well for me. I also noticed that many popular websites like Amazon, Twitter, and Facebook they are using different URI for serving static-contents.To find out this, just right click on any static content and check their URI.

After encountering and going through the same decision making process described here, I decided to go with the ResourceServlet proposal which works out quite nicely. Note that you get more information on how to use webflow in your maven build process here: static.springsource.org/spring-webflow/d... If you use the standard Maven central repository the artifact is (in opposite to the above referred springsource bundle): org.springframework. Webflow spring-js 2.0.9.RELEASE.

I just add three rules before spring default rule (/**) to tuckey's urlrewritefilter (urlrewrite. Xml) to solve the problem / /app/welcome /scripts/** /scripts/$1 /styles/** /styles/$1 /images/** /images/$1 /** /app/$1 /app/** /$1.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions