0

In fact, I am writing a maven project with spring.Everything is ok until I want to import my css file.Here is my web.xml(only the servlet part):

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Then I visit my page on this url:

http://localhost:8080/hello.do?method=reg

However, I have got a problem:style.css is 404 Not Found

so, I have searched some file and then I got a method:add some configuration in my servlet.xml(my servlet is mvc-dispatcher-servlet.xml):

<mvc:resources mapping="resources/**" location="/WEB-INF/resources/" />
<mvc:default-servlet-handler />

As you see my css file is under "/WEB-INF/resources/".But,the problem still exists.At last, I know one thing:

static resources cannot be accessed while servlet is mapped on an URL pattern of / or /*

So I change my web.xml to this:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

Finally, the problem is totally solved,but this makes me more puzzled,why tomcat can not access static resources(js, css) while mapping a front controller servlet on an URL pattern of **.do or / or /*

update(Dec 30 '13):

My application is this:

MyApplication
pom.xml
src
    test
    main
       java
            com.roger.spring
                controller
                    UserController.java
                dao
                    impl
                        UserDaoImpl.java
                    UserDao.java
                domain
                    User.java
                service
                    impl
                        UserService.java
                    UserService.java
        webapp
            WEB-INF
                applicationContext.xml
                dao.xml
                db.properties
                mvc-dispatcher-servlet.xml
                service.xml
                web.xml
                pages
                    index.jsp
                    taglib.jsp
                    user
                         login.jsp
                         reg.jsp
                         profile.jsp
                 resources
                    css
                         style.css
                    js
                         calendar.js
roger
  • 9,063
  • 20
  • 72
  • 119
  • 1
    This statement `static resources cannot be accessed while servlet is mapped on an URL pattern of / or /*` is wrong. – Sotirios Delimanolis Dec 28 '13 at 06:29
  • In fact I am not that clear about this, so I tried to find more articles about this these days.Finally, I find some, but it is a shame to say I cannot understand them totally.But I can tell you how I know this:[How to access static resources when using default servlet](http://stackoverflow.com/a/8717371/2849157) and [Bug 50026 - DefaultServlet serves META-INF and WEB-INF from root when remapped on /folder/*](https://issues.apache.org/bugzilla/show_bug.cgi?id=50026#c2).One thing I am sure is that when I use / or /* or \*.do I can not access my css file,but it wroks when I use /user/*. – roger Dec 30 '13 at 06:11

1 Answers1

0

It would be simpler and rather cleaner to use the default servlet mapping - rather than the /user/* and the struts-like *.do mappings:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

All requests will now be sent through Spring MVC - including requests for static resources.

You should then amend your controller @RequestMappings and remove the *.do too - so your URL becomes:

http://localhost:8080/hello?method=reg

Make sure you still have <mvc:default-servlet-handler /> in your mvc-dispatcher-servlet.xml.

Then you should be able to serve static resources using <mvc:resources mapping="resources/**" location="/WEB-INF/resources/" /> without any problems.

Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • I am sorry I reply late.To make sure,I have tried your method,but in fact, it doesn't work. – roger Dec 30 '13 at 05:08
  • 1
    And for your point,I want to quote an answer for another question([How to access static resources when using default servlet](http://stackoverflow.com/a/12345023/2849157)).The second answer shows that you shouldn't be mapping a front controller servlet on an URL pattern of / or /* at all. – roger Dec 30 '13 at 06:16