ssm配置檔案參考
1.web.xml
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springMVC_mybatis</display-name><!-- 指定spring配置檔案位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param><!-- 配置監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 非同步 -->
<servlet>
<description></description>
<display-name>userservlet</display-name>
<servlet-name>userservlet</servlet-name>
<servlet-class>rigister.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userservlet</servlet-name>
<url-pattern>/userservlet.servlet</url-pattern>
</servlet-mapping>
<!-- 註冊提交 -->
<servlet>
<description></description>
<display-name>addUserSubmit</display-name>
<servlet-name>addUserSubmit</servlet-name>
<servlet-class>rigister.AddUserSubmit</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addUserSubmit</servlet-name>
<url-pattern>/addUserSubmit</url-pattern>
</servlet-mapping>
<!-- 靜態資源 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/images/*</url-pattern>
<url-pattern>/fonts/*</url-pattern>
</servlet-mapping>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- REST支援的springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc-servlet-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-servlet-rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- post亂碼過濾器 -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
2. 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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 對於註解的handler可以單個配置 實際開發中建議使用元件掃描 -->
<!-- 可以掃描contrller,service...... 這裡掃描controller指定其所在的包 -->
<context:component-scan base-package="ssm.controller"></context:component-scan><!-- 靜態資源解析 包括 :js、css、img、.. -->
<!-- <mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/img/" mapping="/img/**"/> -->
<mvc:default-servlet-handler/><!-- 使用mvc:annotation-driven代替上面註解對映器和註解配置器 mvc:annotation-driven預設載入很多引數繫結方法,
比如json轉換解析器預設載入了,如果使用mvc:annotation-driven,不用配置上面. 實際開發中使用mvc:annotation-driven -->
<mvc:annotation-driven conversion-service="conversionService"
validator="validator">
</mvc:annotation-driven><!-- 配置檢視解析器 解析jsp頁面,預設使用jstl標籤,classpath下得有jstl的包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置字首和字尾 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 自動定義引數繫結 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 轉換器 -->
<property name="converters">
<list>
<!-- 日期型別轉換 -->
<bean class="ssm.controller.converter.CustomDateConverter" />
</list>
</property>
</bean>
<!-- 伺服器端校驗 -->
<!-- 校驗器 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- Hibernate校驗器 -->
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<!-- 指定校驗使用的資原始檔,如果不指定則預設使用classpath下的ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
<!-- 校驗錯誤資訊配置檔案 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 資原始檔名 -->
<property name="basenames">
<list>
<value>classpath:CustomValidationMessages</value>
</list>
</property>
<!-- 解決中文亂碼 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 資原始檔編碼格式 -->
<property name="fileEncodings" value="utf-8" />
<!-- 對資原始檔內容快取時間,單位秒 -->
<property name="cacheSeconds" value="120" />
</bean>
<!-- 全域性異常處理器 只要實現HandlerExceptionResolver介面就是全域性異常處理器 -->
<bean class="ssm.exception.CustomExceptiomResolver"></bean>
<!-- 檔案上傳 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設定上傳檔案的最大尺寸為5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean><!--攔截器 -->
<mvc:interceptors>
<!--多個攔截器,順序執行 -->
<!-- 登入認證攔截器 -->
<mvc:interceptor>
<!-- /**表示所有url包括子url路徑 -->
<mvc:mapping path="/**" />
<bean class="ssm.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
3. applicationContext-transaction.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!-- 配置事務
事務管理器
對mybatis操作資料庫的控制,spring使用jdbc的事務控制類-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 資料來源
DataSource在applicationContext-dao.xml配置了-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<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配置 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* ssm.serviceImpl.*.*(..))"/>
</aop:config>
</beans>
4.applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "><!-- 載入配置檔案 -->
<context:property-placeholder location="classpath:db.properties" /><!-- 配置c3p0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入屬性值 -->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean><!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 載入mybatis配置檔案 -->
<property name="configLocation" value="classpath:/mybatis/SqlMapConfig.xml"></property>
<!-- 注入資料來源 -->
<property name="dataSource" ref="dataSource"></property>
</bean><!--1. 配置mapper MapperFactoryBean:根據mapper介面生成代理物件 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> -->
<!-- mapperInterface指定mapper介面 -->
<!-- <property name="mapperInterface" value="ssm.mapper.UserMapper"></property> -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
<!-- </bean> -->
<!--2.通過MapperScannerConfigurer進行mapper掃描(建議使用) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定掃描的包,每個包中間用逗號隔開 -->
<property name="basePackage" value="ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean></beans>