spring3MVC配置

web.xml:

</welcome-file-list>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
    <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>SetCharacterEncoding</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>
  </filter>
  <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

springmvc-servlet.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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/mvc  
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
 <!-- 扫描该包下面的所有的类-->
 <context:component-scan base-package="com.wdh.sm.controller" />
 
 <!-- 启动MVC注解-->
 <mvc:annotation-driven />
 
 <!--  拦截器配置,不拦截static_resource下面(js,css,image) 不拦截所有jsp  
 <mvc:interceptors>
  <mvc:interceptor>
   <mvc:mapping path="/*"/>
   <bean class="com.wdh.sm.filter.LoginFilter"/>
  </mvc:interceptor>
 </mvc:interceptors>
-->
 
 <!-- 静态资源配置,不被拦截-->
 <mvc:default-servlet-handler/>  
 <mvc:resources mapping="/javascript/**" location="/static_resources/javascript/" />
 <mvc:resources mapping="/styles/**" location="/static_resources/css/" />
 <mvc:resources mapping="/images/**" location="/static_resources/images/" />
 
 <!-- 试图配置-->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/pages/" />
  <property name="suffix" value=".jsp" />
 </bean>
 
 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
    <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 --> 
    <property name="defaultErrorView" value="error"></property> 
    <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception --> 
    <property name="exceptionAttribute" value="ex"></property> 
 </bean>
 
 <!-- ajax配置    需要第三方jar:jackson-core-lgpl-1.6.9,jackson-mapper-lgpl-1.6.9-->
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
        <property name="messageConverters"> 
            <list> 
                <ref bean="mappingJacksonHttpMessageConverter" /> 
            </list> 
        </property> 
    </bean> 
    <bean id="mappingJacksonHttpMessageConverter" 
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 

</beans>

 

applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:task="http://www.springframework.org/schema/task"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/task
     http://www.springframework.org/schema/task/spring-task-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/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


      <context:component-scan base-package="com.wdh.sm">

<context:exclude-filter type="regex" expression=".*Controller$" />

</context:component-scan>

 <context:property-placeholder location="classpath:mysql.properties" />
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <!-- 指定连接数据库的JDBC驱动 -->  
        <property name="driverClass" value="${jdbc.driverClassName}">  
        </property>  
        <!-- 连接数据库所用的URL -->  
        <property name="jdbcUrl"  value="${jdbc.url}">  
        </property>  
        <!-- 连接数据库的用户名 -->  
         <property name="user" value="${username}">  
        </property>  
        <!-- 连接数据库的密码 -->  
        <property name="password" value="${password}">  
        </property>  
        <!-- 设置数据库连接池的最大连接数 -->  
        <property name="maxPoolSize" value="20">  
        </property>  
        <!-- 设置数据库连接池的最小连接数 -->  
        <property name="minPoolSize" value="2">  
        </property>  
        <!-- 设置数据库连接池的初始化连接数 -->  
        <property name="initialPoolSize" value="2">  
        </property>  
        <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->  
        <property name="maxIdleTime" value="20">  
        </property>  

 </bean>

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="configLocation" value="WEB-INF/mybatis-config.xml" />
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.wdh.sm.dao"/>
  <property name="markerInterface" value="com.wdh.sm.dao.SqlMapper"/>
 </bean>
  <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
      <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="create*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
      <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
      <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
      <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
      <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 <aop:config>
  <aop:advisor pointcut="execution(public * com.wdh.sm..*.*Service*.*(..))"
   advice-ref="txAdvice" />
 </aop:config>
</beans>

 

你可能感兴趣的:(spring3 mvc 配置)