现在的位置: 首页 > 综合 > 正文

Spring MVC 配置(备忘)

2013年06月02日 ⁄ 综合 ⁄ 共 2435字 ⁄ 字号 评论关闭

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).

抱歉!评论已关闭.