2、web.xml配置

一、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

  1. load-on-startup元素標(biāo)記容器是否在啟動的時候就加載這個servlet(實例化并調(diào)用其init()方法)。
  2. 它的值必須是一個整數(shù),表示servlet應(yīng)該被載入的順序
  3. 當(dāng)值為0或者大于0時,表示容器在應(yīng)用啟動時就加載并初始化這個servlet;
  4. 當(dāng)值小于0或者沒有指定時,則表示容器在該servlet被選擇時才會去加載。
  5. 正數(shù)的值越小,該servlet的優(yōu)先級越高,應(yīng)用啟動時就越先加載。
  6. 當(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("/*");
    }
}

spring-mvc專題

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容