Spring MVC
1.Spring配置文件-applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p=" " xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jd="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<context:annotation-config />
<!--扫描包类,将标注Spring注解的类自动转化为Bean,同时完成Bean的注入-->
<context:component-scan base-package="com.ctrip.controller" />
<context:component-scan base-package="com.ctrip.model" />
<context:component-scan base-package="com.ctrip.service" />
<mvc:annotation-driven />
<!--定义一个bean-->
<bean id="dataBaseConnection" class="com.ctrip.model.DataBaseConnection">
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
</beans>
2. 配置Spring MVC框架
首先要对web.xml文件进行配置,以便Web容器启动时能够自动启动Spring容器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 负责启动Spring容器的 监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 从类路径下加载Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 定义Servlet-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.sp</url-pattern>
</servlet-mapping>
<display-name>SpringProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Spring MVC有一Spring配置文件,文件名为<Servlet名>-servlet.xml,这个配置文件无需通过web.xml的contextConfigLocation上下文参数进行说明,因为Spring MVC de Servlet会自动将它和Spring的其他配置文件进行拼接。

浙公网安备 33010602011771号