天天看點

SpringMVC+Spring+MyBatis 的綜合練習 4 (配置 web.xml )

繼續做基礎配置……這步完事應該可以寫代碼了。呃~~~好像不對,還得先配置并運作 Mybatis Generator 先!!

唉~~漫漫長路……誰能告訴我,究竟會有多少錯~~~停停停,打住!别再犯錯了。

昨天,因為在 applicationContext.xml 中少寫了一個大括号,讓我痛苦地找了幾個小時。就是下面這行:

<property name="password" value="${jdbc.password}"></property>
           

因為少了右邊的大括号,于是password沒能正确傳入,而是傳入了null。于是伺服器就卡死在登入環節了。雖然我用dbForge登入沒問題(廢話,你正确輸入密碼了怎麼會有問題!),但是……算了,不提了,隻能說我太笨了。老了啊~~~~倒是借此機會看了很多文章,知道了報錯的其他可能。

廢話少說,今後再仔細些就是了。

因為是 Web 項目,自然要對 web.xml 進行一系列的配置。之是以從這個開始,是因為它是項目其他配置檔案的源頭,是以我覺得這樣記錄下來,并且今後也按照這個順序配置項目,這樣可以避免遺漏。具體代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    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">

    <!-- 1. 啟動Spring容器 -->

    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- 2. 配置SpringMVC前端控制器,攔截所有請求 -->

    <!-- The front controller of this Spring Web application, responsible for 
        handling all application requests -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 3. 字元編碼過濾器 ,一定要放在所有過濾器之前 -->
    <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>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 4. 使用Rest風格的URI,将頁面普通的post請求轉為指定的delete和put請求 -->
    <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>

</web-app>
           

可以看見,到目前為止,出現了兩個新的需要配置的 xml 檔案:

  • applicationContext.xml: 看上面第12行出現的喲。
  • dispatcherServlet.xml: 這個出現在上面代碼的第27行呢。

如果缺少這兩個配置檔案,Tomcat是拒絕啟動滴~~下篇繼續。

繼續閱讀