一、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
注意事項
路徑和擴展名匹配無法同時設(shè)置
<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元素標(biāo)記容器是否在啟動的時候就加載這個servlet(實例化并調(diào)用其init()方法)。
- 它的值必須是一個整數(shù),表示servlet應(yīng)該被載入的順序
- 當(dāng)值為0或者大于0時,表示容器在應(yīng)用啟動時就加載并初始化這個servlet;
- 當(dāng)值小于0或者沒有指定時,則表示容器在該servlet被選擇時才會去加載。
- 正數(shù)的值越小,該servlet的優(yōu)先級越高,應(yīng)用啟動時就越先加載。
- 當(dāng)值相同時,容器就會自己選擇順序來加載。所以,
<load-on-startup>x</load-on-startup>,中的x取值1,2,3,4,5代表的是優(yōu)先級,而非啟動延遲時間。
四、編碼問題處理
encoding
用來設(shè)置編碼格式 ,過濾post請求 對get請求無效 ,get請求需要修改tomcat/conf/server.xml文件
forceEncoding
用來設(shè)置是否理會 request.getCharacterEncoding()方法,設(shè)置為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);
}
五、服務(wù)器錯誤界面(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>
<!-- 僅僅在調(diào)試的時候注視掉,在正式部署的時候不能注釋 -->
<!-- 這樣配置也是可以的,表示發(fā)生500錯誤的時候,轉(zhuǎn)到500.jsp頁面處理。 -->
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<!-- 這樣的配置表示如果jsp頁面或者servlet發(fā)生java.lang.Exception類型(當(dāng)然包含子類)的異常就會轉(zhuǎn)到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>
<!--
當(dāng)error-code和exception-type都配置時,exception-type配置的頁面優(yōu)先級高
及出現(xiàn)500錯誤,發(fā)生異常Exception時會跳轉(zhuǎn)到500.jsp
-->
六、處理RESTful的DELETE PUT請求
<!-- 將POST請求轉(zhuǎn)化為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>
七、設(shè)置默認首頁
<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("/*");
}
}