Spring+SpringMVC的整合 具体的开发步骤和xml文件配置

xiaoxiao2021-02-28  108

1.导入Jar包。

c3p0-0.9.1.2.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-logging-1.1.3.jar mysql-connector-java-5.1.37-bin.jar spring-aop-4.0.0.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar spring-jdbc-4.0.0.RELEASE.jar spring-orm-4.0.0.RELEASE.jar spring-tx-4.0.0.RELEASE.jar spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar

一共17个。下载地址:链接:http://pan.baidu.com/s/1sl6YKkH 密码:fvyw

2.配置web.xml文件。

(1)配置Spring的相关信息。

安装SpringTool插件后,按住alt + / 后倒数第三个以listener结尾的就是Spring的配置。

<!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 此处为Spring的xml配置文件的路径 需要更改 classpath代表根目录 --> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

(2)配置SpringMVC的相关信息。

A、关联SpringMVC的xml文件。

同样,按住alt + / 倒数第二个就是。是SpringMVC的前端控制器。

<!-- 配置SpringMVC --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 此处为SpringMVC的xml配置文件的路径 需要更改 classpath代表根目录 --> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping><servlet-name>springDispatcherServlet</servlet-name> 〈!-- 此处是SpringMVC前端控制器的映射路径,一般为:/--> <url-pattern>/</url-pattern> </servlet-mapping>

B、配置字符编码过滤器

可以处理POST请求的乱码。GET请求的乱码已经在Tomcat的配置文件中进行了设置。

<!-- 配置字符编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

C、POST请求转PUT和DELETE请求的过滤器

<!-- POST转PUT和DELETE --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 至此,web.xml文件配置完毕,其中Spring有一处Listener,SpringMVC有一个前端控制器,一个字符过滤器、一个请求转化过滤器。共配置四次。

完整的web.xml文件如下: web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ss-integrated</display-name> <!-- 配置Spring --> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 此处为Spring的xml配置文件的路径 需要更改 classpath代表根目录 --> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置SpringMVC --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置字符编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- POST转PUT和DELETE --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

3.配置spring的配置文件

我的文件名为:applicationContext.xml

(1)配置扫描包

<!-- 配置扫描包 --> <context:component-scan base-package="com.gpf.ss" use-default-filters="true"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> Spring和SpringMVC都有一个各自的IOC容器,为了各自控制各自的,一般约定为SpringMVC控制Dao层和Service层,SpringMVC控制Controller层,所以设置Spring的扫描包不包括Controller,下面在SpringMVC的配置文件中只包括Controller文件,需要设置use-default-filters为false.即不自动扫描,只扫描指定包,而Spring配置文件指明不扫描的包,所以需要自动扫描。

(2)加载数据库信息并配置数据库连接池

<!-- 加载数据库文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 获取信息 --> <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> </bean> 我的数据库文件如下: db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/neuedu jdbc.username=root jdbc.password=123456

(3)配置JdbcTemplate。

<!-- 配置JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="comboPooledDataSource"></property> </bean>

(4)配置使用参数名的Template

<!-- 配置参数名的Template --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg name="dataSource" ref="comboPooledDataSource"></constructor-arg> </bean>

(5)配置并加载事务

<!-- 配置事务 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="comboPooledDataSource"></property> </bean> <!-- 加载事务 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> 至此,Spring的xml配置文件结束,共5步。完整代码如下: 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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 配置扫描包 --> <context:component-scan base-package="com.gpf.ss" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加载数据库文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 获取信息 --> <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> </bean> <!-- 配置JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="comboPooledDataSource"></property> </bean> <!-- 配置参数名的Template --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg name="dataSource" ref="comboPooledDataSource"></constructor-arg> </bean> <!-- 配置事务 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="comboPooledDataSource"></property> </bean> <!-- 加载事务 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> </beans>

4.配置springmvc.xml文件。

(1)配置扫描包

<!-- 配置扫描包 --> <context:component-scan base-package="com.gpf.ss" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>

(2)配置视图解析器

<!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean>

(3)配置其他常用配置。

<!--设置可以访问静态资源 否则文件无法访问 --> <mvc:default-servlet-handler /> <!-- 设置view-controller之后,不设置此方法之前的RequestMapping方法设置的URL无法访问 --> <mvc:annotation-driven></mvc:annotation-driven> 配置完毕,完整代码如下:springmvc.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置扫描包 --> <context:component-scan base-package="com.gpf.ss" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--设置可以访问静态资源 否则文件无法访问 --> <mvc:default-servlet-handler /> <!-- 设置view-controller之后,不设置此方法之前的RequestMapping方法设置的URL无法访问 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>

至此,配置文件配置完毕,接下来就按照正常的开发流层,Dao层、Service层、Controller层。

转载请注明原文地址: https://www.6miu.com/read-85536.html

最新回复(0)