Spring MVC web.xml 配置文件配置

新建立一个Web Project, 含有web.xml

[html] view plaincopy
01.<?xml version="1.0"encoding="UTF-8"?> 
02.<web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
03.<listener> 
04.<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
05.</listener> 
06.<context-param> 
07.<param-name>contextConfigLocation</param-name> 
08.<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/controllers.xml,/WEB-INF/service.xml</param-value> 
09.</context-param> 
10.<servlet> 
11.<servlet-name>dispatch</servlet> 
12.<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
13.<init-param> 
14.<param-name>contextConfigLocation</param-name> 
15.<param-value>/WEB-INF/applicationContext.xml</param-value> 
16.</init-param> 
17.</servlet> 
18.<servlet-mapping> 
19.<servlet-name>dispatch</servlet-name> 
20.<servlet-pattern>*.*</servlet-pattern> 
21.</servlet-mapping> 
22.</web-app> 


Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xml。
当需要载入多个spring相关的配置文件时,首先加载ContextLoaderListener类,再指定context-param中指定多个spring配置文件,使用逗号分别隔开各个文件。为了使用方便可以将配置文件进行MVC式的分解,配置控制器Bean的配置文件放置在一个xml文件中,server的Bean放在service.xml文件中。
<servlet-mapping>指定的该servlet接管的url的行为,此处为了简便起见使用*.*,则表示在URL只要是在本机使用的任何request都是由该dispatchServlet来处理。

猜你喜欢

转载自weitao1026.iteye.com/blog/2265303
今日推荐