Spring MVC 配置(备忘)

WEB-INF/spring/appServlet/servlet-context.xml

01 <?xml version="1.0" encoding="UTF-8"?>
06     xsi:schemaLocation="
10   
11     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
12   
13     <!-- Scans within the base package of the application for @Components to configure as beans -->
14     <!-- @Controller, @Service, @Configuration, etc. -->
15     <context:component-scan base-package="xyz.sample.baremvc" />
16   
17     <!-- Enables the Spring MVC @Controller programming model -->
18     <mvc:annotation-driven />
19   
20 </beans>
 

WEB-INF/web.xml

01 <?xml version="1.0" encoding="UTF-8"?>
02 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
05   
06     <!-- Processes application requests -->
07     <servlet>
08         <servlet-name>appServlet</servlet-name>
09         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10         <init-param>
11             <param-name>contextConfigLocation</param-name>
12             <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
13         </init-param>
14         <load-on-startup>1</load-on-startup>
15     </servlet>        
16   
17     <servlet-mapping>
18         <servlet-name>appServlet</servlet-name>
19         <url-pattern>/</url-pattern>
20     </servlet-mapping>
21 </web-app>

A number of things are being done here:

  • We register the DispatcherServlet as as a Servlet called appServlet
  • We map this Servlet to handle incoming requests (relative to the app path) starting with "/"
  • We use the ContextConfigLocation init parameter to customize the location for the base configuration XML file for the Spring Application Context that is loaded by the DispatcherServlet, instead of relying on the default location of <servletname>-context.xml).
posted @ 2011-04-05 16:36  李克华  阅读(734)  评论(0编辑  收藏  举报