Spring MVC 配置(备忘)
WEB-INF/spring/appServlet/servlet-context.xml
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xmlns:mvc = "http://www.springframework.org/schema/mvc" |
05 |
xmlns:context = "http://www.springframework.org/schema/context" |
06 |
xsi:schemaLocation=" |
07 |
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd |
08 |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
09 |
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
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" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > |
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).