spring+spring mvc+spring jdbc

一、基本环境:myeclipse8.5、tomcat 6.0.4、spring 3.1.1

二、搭建步骤

1、在web.xml里配置spring应用上下文和spring mvc的核心servlet,代码如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <welcome-file-list><welcome-file>index.jsp</welcome-file> </welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet> <servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern> </servlet-mapping></web-app>

2、在src目录下配置applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="" xmlns:xsi="" xmlns:aop="" xmlns:tx="" xmlns:context="" xsi:schemaLocation=""><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"lazy-init="false"><property name="locations"><list><value>classpath:jdbc.properties</value></list></property></bean><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url"><value>${jdbc.url}</value></property><property name="username"><value>${jdbc.username}</value></property><property name="password"><value>${jdbc.password}</value></property><property name="maxActive"><value>20</value></property><property name="testOnBorrow"><value>true</value></property><property name="validationQuery"><value>SELECT 'x' FROM DUAL</value></property><property name="connectionProperties"><value>useUnicode=true;characterEncoding=utf-8</value></property></bean><!– 配置spring-jdbcTemplate模板 –><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!– 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现–><bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!– 事物控制 –><bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"><property name="transactionManager" ref="dataSourceTransactionManager" /><property name="transactionAttributes"><props><prop key="terParamsFileLeadingIn">PROPAGATION_REQUIRED</prop><prop key="batchSave">PROPAGATION_REQUIRED</prop><prop key="*">readOnly</prop></props></property></bean><!– spring注解模式配置 –><context:annotation-config/><context:component-scan base-package="cn.eshore"/> </beans>

spring配置文件中配置了数据源、spring jdbc、事务控制等等,另外采用了spring的注解配置,所以增加了最后两行。

3、配置spring mvc配置文件,文件默认位于web-info目录下,,名称为web.xml中指定的servlet的名字springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="" xmlns:xsi="" xmlns:p="" xmlns:context="" xmlns:mvc="" xsi:schemaLocation=" "><!– 启用spring mvc 注解 <context:annotation-config />–> <mvc:annotation-driven /><!– 设置使用注解的类所在的jar包 –><context:component-scan base-package="cn.eshore.web"/><bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><props><prop key="java.lang.Exception">error/error</prop><prop key="java.lang.Throwable">error/error</prop></props></property><property name="statusCodes"><props><prop key="error/error">500</prop><prop key="error/error">404</prop></props></property><!– 设置日志输出级别,不定义则默认不输出警告等错误日志信息 –><property name="warnLogCategory" value="WARN"></property><!– 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 –><property name="defaultErrorView" value="error/error"></property><!– 默认HTTP状态码 –><property name="defaultStatusCode" value="500"></property></bean><!– 定义跳转的文件的前后缀 ViewResolver –> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> </beans>如果需要在controller里返回json数据,则需要启用<mvc:annotation-driven />

一般程序启用<context:annotation-config />即可,此段代码已在spring配置文件中配置,则此处无需配置。

4、编写测试的controlller

我是在旅行吗?也许是的。

spring+spring mvc+spring jdbc

相关文章:

你感兴趣的文章:

标签云: