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