0%

JAVA-Spring WebSite for Eclipse for XML 設定

JAVA-Spring WebSite for Eclipse for XML 設定

Web.xml 設定

  • <context-param>mvc-config.xml 設定在contextConfigLocation
    1
    2
    3
    4
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-config.xml</param-value>
    </context-param>
  • 在設定 <listener>
    1
    2
    3
       <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    P.S. 有在 <context-param> 設定contextConfigLocation,就要設定<listener>

For MVC

  • <servlet> 設定 DispatherServlet,建立前端控制器(front controller)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <servlet>
    <servlet-name>translator</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    contextConfigLocation 尚未設定時,預設會讀取 Servlet名稱- servlet.xml

  • <servlet-mapping> 設定 Controller條件

    1
    2
    3
    4
       <servlet-mapping>
    <servlet-name>translator</servlet-name>
    <url-pattern>*.action</url-pattern>
    </servlet-mapping>
  • 流程

    1. client request
    2. servlet-mapping 驗證 url,是否有符合
    3. 連結到 mapping 的 dispatcherServlet
    4. 根據 dispatcher-servlet.xml 連接到 Controller