注:其中的url-pattern最好使用.do,但是有时候开发android工程的时候,请求可能为resetfull格式,就不能使用这个url-pattern,这时使用 / 作为url-pattern,但是,对于其静态资源的的请求,中央调度器不能为它找到相应的处理器,就会抛出404错误,那么这时后有几种方法解决这个问题,这里推荐3种,前两种是通过tomcat中的web.xml文件里的默认处理器来处理静态资源的,最后一个是在servlet3.0之后通过另一个处理器来处理静态资源的,第三种方式不是很好,当静态资源种类太多时,就可能比较繁琐。 下面一个工程包含三种方式*
1、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>01-springMVCPrimary</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 注册中央调度器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springMVC的配置文件的位置和路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!-- 表示启动服务器就创建该对象,不设置的话,那么就在请求到达时,创建该对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 方式1 -->
<!--
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
-->
</web-app>
2、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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 方式2:底层还是调用了defaultServlet -->
<!--
<mvc:default-servlet-handler/>
-->
<!-- 方式3:从servlet3.0之后开始用,tomcat平台新提供一种处理器用于处理请求,
location:表示请求资源路径 mapping:表示请求资源匹配名称,该方式不太好用,比较麻烦 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
</beans>
3、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<img alt="图片" src="images/1.jpg">
</body>
</html>