web框架之Spring-MVC环境搭建

1、新建web项目,并在web.xml加入spring mvc的servlet

<!-- spring mvc容器和servlet的定义 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 这里的参数如果不配置,则默认查找web-inf下的{servlet-name}-servlet.xml文件 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springMVC.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>

 2、加入spring mvc的依赖jar包

备注:我这里除了spring的jar包外还添加了一些其他的功能(quartz、JSR303的hibernate实现、c3p0连接池、DB2驱动、spring json的支持(jackson)),这里并未完全加入spring的包,可根据项目的需要增减jar.

3、配置spring mvc的配置文件。

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"
  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4     xmlns:tx="http://www.springframework.org/schema/tx"
  5     xmlns:context="http://www.springframework.org/schema/context"
  6     xmlns:aop="http://www.springframework.org/schema/aop" 
  7     xmlns:mvc="http://www.springframework.org/schema/mvc"
  8     xmlns:task="http://www.springframework.org/schema/task"
  9     xsi:schemaLocation="http://www.springframework.org/schema/beans    
 10     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
 11     http://www.springframework.org/schema/tx    
 12     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
 13     http://www.springframework.org/schema/context   
 14     http://www.springframework.org/schema/context/spring-context-4.0.xsd 
 15     http://www.springframework.org/schema/aop
 16     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
 17     http://www.springframework.org/schema/mvc   
 18     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 19     http://www.springframework.org/schema/task
 20     http://www.springframework.org/schema/task/spring-task-4.0.xsd">
 21 
 22     <!-- Spring MVC配置 -->
 23     <context:annotation-config />
 24     <!--扫描注解 -->
 25     <context:component-scan base-package="com.tf" />
 26     <!--默认的mvc注解映射的支持 -->
 27     <mvc:annotation-driven/>
 28     <!-- 支持异步方法执行 -->
 29     <task:annotation-driven /> 
 30 
 31     <!-- 视图解析器和json解析器 -->
 32     <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 33         <property name="mediaTypes">
 34             <map>
 35                 <entry key="html" value="text/html"/>
 36                 <entry key="json" value="application/json"/>
 37             </map>
 38         </property>
 39         <property name="viewResolvers">
 40             <list>
 41                 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 42                     <property name="prefix" value="/WEB-INF/jsp/" /> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
 43                     <property name="suffix" value=".jsp"/>
 44                 </bean>
 45             </list>
 46         </property>
 47         <property name="defaultViews">
 48             <list>
 49                 <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
 50             </list>
 51         </property>
 52     </bean>
 53     
 54     <!-- 文件上传解析器 -->
 55     <bean id="multipartResolver"
 56         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 57         <!-- one of the properties available; the maximum file size in bytes -->
 58         <property name="maxUploadSize" value="-1"/>
 59     </bean>
 60 
 61     <!-- 总错误处理 -->
 62     <bean id="exceptionResolver"
 63         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
 64         <property name="defaultErrorView">
 65             <value>/error</value>
 66         </property>
 67         <property name="defaultStatusCode">
 68             <value>500</value>
 69         </property>
 70         <property name="warnLogCategory">
 71             <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
 72             </value>
 73         </property>
 74     </bean>
 75 
 76     <!-- 对静态资源文件的访问 -->
 77     <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926" />
 78     <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />
 79     <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />
 80 
 81 
 82     <!-- 数据库和事务配置 -->
 83 
 84     <!-- 加载配置文件 -->
 85     <context:property-placeholder location="classpath:jdbc.properties" />
 86     <!-- 定义数据源 -->
 87     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 88         destroy-method="close">
 89         <property name="driverClass">
 90             <value>${jdbc.driverClass}</value>
 91         </property>
 92         <property name="jdbcUrl">
 93             <value>${jdbc.url}</value>
 94         </property>
 95         <property name="user">
 96             <value>${jdbc.username}</value>
 97         </property>
 98         <property name="password">
 99             <value>${jdbc.password}</value>
100         </property>
101         <!--连接池中保留的最小连接数。 -->
102         <property name="minPoolSize">
103             <value>${c3p0.minPoolSize}</value>
104         </property>
105         <!--连接池中保留的最大连接数。Default: 15 -->
106         <property name="maxPoolSize">
107             <value>${c3p0.maxPoolSize}</value>
108         </property>
109         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
110         <property name="initialPoolSize">
111             <value>${c3p0.initialPoolSize}</value>
112         </property>
113         <!--每30秒检查所有连接池中的空闲连接。Default: 0 -->
114         <property name="idleConnectionTestPeriod">
115             <value>${c3p0.idleConnectionTestPeriod}</value>
116         </property>
117     </bean>
118     <!-- weblogic推荐使用jndi连接池 -->
119     <!-- 
120     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
121         <property name="jndiName"> 
122             <value>java:comp/env/jdbc/myDatasource</value> 
123         </property> 
124     </bean> 
125     -->
126 
127     <!-- 定义jdbcTemplate -->
128     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
129         <property name="dataSource">
130             <ref bean="dataSource"/>
131         </property>
132     </bean>
133     <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
134         <constructor-arg><ref bean="dataSource"/></constructor-arg> 
135     </bean>
136 
137     <!-- 定义事务管理器 -->
138     <bean id="transactionManager"
139         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
140         <property name="dataSource">
141             <ref bean="dataSource" />
142         </property>
143     </bean>
144 
145     <!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required -->
146     <tx:advice id="txAdvice" transaction-manager="transactionManager">
147         <tx:attributes>
148             <tx:method name="save*" propagation="REQUIRED" />
149             <tx:method name="insert*" propagation="REQUIRED" />
150             <tx:method name="delete*" propagation="REQUIRED" />
151             <tx:method name="update*" propagation="REQUIRED" />
152             <tx:method name="login*" propagation="REQUIRED" />
153             <tx:method name="regist*" propagation="REQUIRED" />
154             <tx:method name="*" read-only="true" />
155         </tx:attributes>
156     </tx:advice>
157 
158     <!-- 配置那些类的方法进行事务管理 -->
159     <aop:config>
160         <aop:pointcut id="allManagerMethod" expression="execution (* com.tf.*.service.*.*(..))" />
161         <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
162     </aop:config>
163 </beans>

 

posted @ 2016-07-30 16:53  Ericki  阅读(279)  评论(0编辑  收藏  举报