首页 > 代码库 > 最新SpringMVC+Spring4.1.1+hibernate4.3.5+maven 整合详细楝及说明

最新SpringMVC+Spring4.1.1+hibernate4.3.5+maven 整合详细楝及说明

最新SpringMVC+Spring4.1.1+hibernate4.3.5+maven 整合详细配置


app主配置web.xml文件如下:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Spring4 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring.applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring4.0 MVC配置 -->
<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/spring.web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>springMVC</servlet-name>
</filter-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- 错误处理 -->
<error-page>
<error-code>403</error-code>
<location>/jsp/error/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/jsp/error/404.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/jsp/error/405.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/jsp/error/500.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- <filter> <filter-name>sessionFilter</filter-name> <filter-class>com.everich.everichscm.filter.SessionFilter</filter-class> 
</filter> <filter-mapping> <filter-name>sessionFilter</filter-name> <url-pattern>/*</url-pattern> 
</filter-mapping> -->

<!-- 下载保存 -->

<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>

<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>

<mime-mapping>
<extension>xlsx</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>

<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>

</web-app>


spring配置文件:spring.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	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/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
      					http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring   
      					http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">


	<!-- AOP 自动代理 -->
	<aop:aspectj-autoproxy />
	<context:property-placeholder location="classpath:/spring/*.properties" />
	<context:annotation-config />
	<context:component-scan base-package="com.everich.everichscm"
		use-default-filters="true">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<bean id="dataSource" name="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="http://www.mamicode.com/${basic.driverClassName}" />
		<property name="url" value="http://www.mamicode.com/${basic.url}" />
		<property name="username" value="http://www.mamicode.com/${basic.username}" />
		<property name="password" value="http://www.mamicode.com/${basic.password}" />
	</bean>
	 <bean id="dataSourceSql" name="dataSourceSql"
		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="http://www.mamicode.com/${mssql.driverClassName}" />
		<property name="url" value="http://www.mamicode.com/${mssql.url}" />
		<property name="username" value="http://www.mamicode.com/${mssql.username}" />
		<property name="password" value="http://www.mamicode.com/${mssql.password}" />
	</bean>  	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.everich.everichscm.entity</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${basic.dialect}</prop>
				<prop key="hibernate.show_sql">${basic.show_sql}</prop>
				<prop key="hibernate.format_sql">${basic.format_sql}</prop>
				<prop key="hbm2ddl.auto">${basic.hbm2ddl.auto}</prop>
				<prop key="hibernate.hbm2ddl.auto">${basic.hibernate.hbm2ddl.auto}</prop>
				<!-- 配置缓存提供商 -->
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<!-- 开启二级缓存 -->
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<!-- hibernate的查询缓存 -->
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<!-- 指定ehcache.xml的位置 -->
				<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
			</props>
		</property>
	</bean>
	
	<bean id="exceptionTranslator"
		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager" />
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:advice id="transactionAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>	
</beans>


springMVC配置文件:spring.web.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
				        http://www.springframework.org/schema/beans
				        http://www.springframework.org/schema/beans/spring-beans.xsd
				        http://www.springframework.org/schema/mvc
				        http://www.springframework.org/schema/mvc/spring-mvc.xsd
				        http://www.springframework.org/schema/context
				        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:annotation-config />
	<!-- spring mvc 扫描设置: 与spring容器分开加载 -->
	<context:component-scan base-package="com.everich.everichscm"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<!-- 匹配 Controller -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
	<!-- 使用annocation创建Controller的bean -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

	<!-- rest形式的视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="mediaTypes">
			<map>
				<entry key="json" value="http://www.mamicode.com/application/json" />
				<entry key="xml" value="http://www.mamicode.com/text/xml" />
				<entry key="htm" value="http://www.mamicode.com/text/html" />
			</map>
		</property>
		<property name="defaultContentType" value="http://www.mamicode.com/text/html"></property>
		<property name="favorParameter" value="http://www.mamicode.com/true"></property>
	</bean>

	<!-- 国际化支持 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename">
			<value>global,systemMgr</value>
		</property>
	</bean>
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="http://www.mamicode.com/org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="http://www.mamicode.com/jsp/"></property>
		<property name="suffix" value="http://www.mamicode.com/.jsp"></property>
		<property name="order" value="http://www.mamicode.com/2"></property>
	</bean>
	<bean id="tiles_viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="http://www.mamicode.com/org.springframework.web.servlet.view.tiles2.TilesView" />
		<property name="order" value="http://www.mamicode.com/1"></property>
	</bean>
	<bean id="tilesConfigurer"
		class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
		<property name="definitions">
			<list>
				<value>/jsp/tiles/tiles-definition.xml</value>
			</list>
		</property>
	</bean>
	<!--启动Spring MVC的注解功能,设置编码方式,防止乱码 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>

	<!-- 异常处理 -->
	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="com.everich.everichscm.util.exception.SystemException">error/500</prop>
				<prop key="com.everich.everichscm.util.exception.BusinessException">error/errorpage</prop>
				<prop key="java.lang.exception">error/500</prop>
			</props>
		</property>
	</bean>
</beans>


项目的pom.xml文件,由于内容过多,只粘部分内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>app</groupId>
	<artifactId>app</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>app</name>
	<url>http://maven.apache.org</url>
	<properties>
		<spring-version>4.1.1.RELEASE</spring-version>
		<hibernate-version>3.6.1.Final</hibernate-version>
		<tiles-version>2.2.2</tiles-version>
		<slf4j-version>1.5.8</slf4j-version>
		<jackson-version>1.9.13</jackson-version>
		<jstl-version>1.1.2</jstl-version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>net.sourceforge.jtds</groupId>
			<artifactId>jtds</artifactId>
			<version>1.2.4</version>
		</dependency>
		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.5.2</version>
		</dependency>
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>activation</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.integration</groupId>
			<artifactId>spring-integration-mail</artifactId>
			<version>4.1.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.6.9</version>
		</dependency>
		<dependency>
			<groupId>com.googlecode.ehcache-spring-annotations</groupId>
			<artifactId>ehcache-spring-annotations</artifactId>
			<version>${jstl-version}</version>
		</dependency>

		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl-version}</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${jstl-version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.16.3</version>
		</dependency>
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.5.0-b01</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-version}</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>${hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-nop</artifactId>
			<version>${slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>


最新SpringMVC+Spring4.1.1+hibernate4.3.5+maven 整合详细楝及说明