静态资源访问前言:
web.xml
<mvc:resources mapping="/**" location="classpath:/views/"/>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置前端控制器-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<!-- 使得容器只要启动后,就初始化此servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
</web-app>
SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd>
">
<context:component-scan base-package="com.spring.controller"/>
</beans>
Hello.controller
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello1(HttpServletResponse response) throws IOException {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test.jsp");
return modelAndView;
}
}
启动浏览器访问:
http://localhost:8080/hello1.do
优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往使用 *.do 、 *.xhtml等方式,如果将DispatcherServlet请求映射配置为"/",则Spring MVC将捕获Web容器所有的请求,包括静态资源的请求,SpringMVC访问静态资源(img,css,js等(不包括jsp和html)),因为在web.xml 中拦截了所有请求,<url-pattern>/</url-pattern>,所以请求全都转到了DispatcherServlet中处理,导致静态资源无法访问(浏览器直接访问或jsp里面引用都找不到)。如何让Spring框架能够捕获所有URL的请求,同时又将静态资源的请求转由Web容器处理?
解决静态资源无法访问的问题
此问题有俩种 解决办法:
第一种:在SpringMVC配置文件中做如下配置,表示webapp/test2/css/下的所有文件可以直接按路径访问,不交给DispatcherServlet处理。
<mvc:resources mapping="/test2/css/**" location="/test2/css/"/>
<mvc:resources mapping="/**" location="classpath:/views/"/>这种方式可以将资源放在任何位置。还可以将静态配置文件写在resources下
第二种:在SpringMVC配置文件中做如下配置
<mvc:default-servlet-handler/>
会在Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。
<mvc:default-servlet-handler/>(此行加了以后一定要加<mvc:annotation-driven>),<mvc:annotation-driven>默认会帮我们注册默认处理请求,参数和返回值的类,其中最主要的两个类:DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter ,分别为HandlerMapping的实现类和HandlerAdapter的实现类,从3.1.x版本开始对应实现类改为了RequestMappingHandlerMapping和RequestMappingHandlerAdapter。不加的话会注入默认的俩个处理器和适配器。不适用,报404(可以通过调试DispatcherServlet看出)
最终的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置前端控制器-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<!-- 使得容器只要启动后,就初始化此servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
</web-app>
最终的SpringiMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.spring.controller"/>
<!--主要针对放在WEB-INF下不能直接访问需要controller转发的资源-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
最终的HelloController
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello1(HttpServletResponse response) throws IOException {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test.jsp");
return modelAndView;
}
}