首先,maven工程中的web.xml配置文件不是必须的。
<?xml version="1.0" encoding="UTF-8"?> //xml的版本号为1.0,字符编码为utf-8
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/javaee ">
/**
* ContextLoaderListener加载内容
* context-param用来声明应用范围(整个WEB项目)内的上下文初始化参数。
* @param param-name 设定上下文的参数名称。必须是唯一名称
* @param param-value 设定的参数名称的值
*/
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
/**
* DispatcherServlt加载内容
*
*/
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
//指定欢迎页面(个人理解这不是首页吗,欢迎页其实我一点都不欢迎你来访问)
<welcome-file-list>
<welcome-file>index.html</welcome-file> //javaweb项目默认index.html是首页
......
//可设置多个页面,从上往下,依次查找,找到就显示,结束这次任务
<welcome-file>user/index.html</welcome-file>
//可对不同的文件设置自身的首页
</welcome-file-list>
//错误编码,跳转自定义页面
<error-page>
<error-code>404</error-code>
<location>/user/8888.html</location>
</error-page>
//抛异常
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.html<location>
</error-page>
//过滤器解决spring编码问题,只对post请求有效
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
</web-app>