一、web.xml常用配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd" version="3.0">
<display-name>Application</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--过滤post请求 对get请求无效 get请求需要修改tomcat/conf/server.xml文件-->
<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>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<!--<url-pattern>*.do</url-pattern>-->
</filter-mapping>
</web-app>
二、DispatcherServlet注册
url-pattern
匹配原则
1、精确匹配 /account/login
2、通配符匹配 /*
3、后缀匹配/*.do
注意事项
路径和扩展名匹配无法同时设置
<url-pattern>/xxx/*.jsp</url-pattern>
<url-pattern>/*.jsp</url-pattern>
<url-pattern>xx*.jsp</url-pattern>
精确匹配,url必须是 /xxx/*
/xxx,这里的*不是通配的含义
<url-pattern>/xxx/*/xxx</url-pattern>
load-on-startup
- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
- 它的值必须是一个整数,表示servlet应该被载入的顺序
- 当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
- 当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
- 正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
- 当值相同时,容器就会自己选择顺序来加载。所以,
<load-on-startup>x</load-on-startup>
,中的x取值1,2,3,4,5代表的是优先级,而非启动延迟时间。
四、编码问题处理
encoding
用来设置编码格式 ,过滤post请求 对get请求无效 ,get请求需要修改tomcat/conf/server.xml文件
forceEncoding
用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式
源码片段
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String encoding = getEncoding();
if (encoding != null) {
if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
request.setCharacterEncoding(encoding);
}
if (isForceResponseEncoding()) {
response.setCharacterEncoding(encoding);
}
}
filterChain.doFilter(request, response);
}
五、服务器错误界面(error-page)
<!-- 默认的错误处理页面 -->
<error-page>
<error-code>403</error-code>
<location>/403.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 -->
<!-- 这样配置也是可以的,表示发生500错误的时候,转到500.jsp页面处理。 -->
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<!-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型(当然包含子类)的异常就会转到500.jsp页面处理。 -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/500.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.jsp</location>
</error-page>
<!--
当error-code和exception-type都配置时,exception-type配置的页面优先级高
及出现500错误,发生异常Exception时会跳转到500.jsp
-->
六、处理RESTful的DELETE PUT请求
<!-- 将POST请求转化为DELETE或者是PUT 要用_method指定真正的请求方法 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决PUT请求无法提交Form的问题 -->
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
七、设置默认首页
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>home.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
八、通过编码方式初始化
public class AppWebApplicationInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet());
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
}