struts2+spring+hibernate web项目参考示例
Posted suheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2+spring+hibernate web项目参考示例相关的知识,希望对你有一定的参考价值。
web配置
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/daoComponentConfigure.xml,/WEB-INF/serviceComponentConfigure.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
spring配置,这里拆成了三个文件
applicationContext.xml
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:aspectj-autoproxy proxy-target-class="true" /> <aop:config> <aop:pointcut id="allPointcut" expression="within(com.ehe.commonDaoImpl..*)||within(com.ehe.daoImpl..*)||within(com.ehe.service..*)||within(test..*)" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allPointcut" /> </aop:config> </beans>
daoComponentConfigure.xml
<?xml version="1.0" encoding="GBK"?> <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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost:3306/OnlineTask" p:user="test" p:password="492159" p:maxPoolSize="200" p:minPoolSize="2" p:initialPoolSize="2" p:maxIdleTime="20" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> <property name="annotatedClasses"> <list> <value>com.ehe.domain.Applicant</value> <value>com.ehe.domain.ApplicantLeader</value> <value>com.ehe.domain.Employee</value> <value>com.ehe.domain.Engineer</value> <value>com.ehe.domain.EngineerLeader</value> <value>com.ehe.domain.Remark</value> <value>com.ehe.domain.Job</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.hbm2ddl.auto=update hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.format_sql=true #开启二级缓存 hibernate.cache.use_second_level_cache=true #设置二级缓存的提供者 hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory </value> </property> </bean> <bean id="daoTemplate" abstract="true" lazy-init="true" p:sessionFactory-ref="sessionFactory" /> <bean id="commonDao" class="com.ehe.commonDaoImpl.CommonDaoImpl" parent="daoTemplate" /> <bean id="applicantDao" class="com.ehe.daoImpl.ApplicantDaoImpl" parent="daoTemplate" /> <bean id="applicantLeaderDao" class="com.ehe.daoImpl.ApplicantLeaderDaoImpl" parent="daoTemplate" /> <bean id="engineerDao" class="com.ehe.daoImpl.EngineerDaoImpl" parent="daoTemplate" /> <bean id="engineerLeaderDao" class="com.ehe.daoImpl.EngineerLeaderDaoImpl" parent="daoTemplate" /> <bean id="remarkDao" class="com.ehe.daoImpl.RemarkDaoImpl" parent="daoTemplate" /> <bean id="jobDao" class="com.ehe.daoImpl.JobDaoImpl" parent="daoTemplate" /> </beans>
serviceComponentConfigure.xml
<?xml version="1.0" encoding="GBK"?> <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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="serviceTemplate" abstract="true" lazy-init="true" p:applicantDao-ref="applicantDao" p:applicantLeaderDao-ref="applicantLeaderDao" p:engineerDao-ref="engineerDao" p:engineerLeaderDao-ref="engineerLeaderDao" p:remarkDao-ref="remarkDao" p:jobDao-ref="jobDao" p:commonDao-ref="commonDao" /> <bean id="applS" class="com.ehe.service.ApplicantLeaderService" parent="serviceTemplate" /> <bean id="appS" class="com.ehe.service.ApplicantService" parent="serviceTemplate" /> <bean id="empS" class="com.ehe.service.EmployeeService" parent="serviceTemplate" /> <bean id="engS" class="com.ehe.service.EngineerService" parent="serviceTemplate" /> <bean id="englS" class="com.ehe.service.EngineerLeaderService" parent="serviceTemplate" /> </beans>
struts2配置
struts.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.multipart.saveDir" value="/upload" /> <package name="default" extends="struts-default"> <interceptors> <interceptor name="preProcess" class="com.ehe.interceptor.Preprocess" /> <interceptor-stack name="myDefaultStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="preProcess" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="myDefaultStack" /> <global-results> <result name="input">/WEB-INF/content/login.jsp</result> <result name="login">/WEB-INF/content/login.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </global-results> <action name="base" class="com.ehe.action.BaseAction"> <result name="showSingleJob">/WEB-INF/content/showSingleJob.jsp</result> <result name="download" type="stream"> <param name="contentType">${contentType}</param> <param name="inputName">targetSource</param> <param name="contentDisposition">fileName="${downloadFileName}"</param> <param name="bufferSize">4096</param> </result> </action> <action name="userLogin" class="com.ehe.action.LoginAction"> <result name="app" type="chain">applicant</result> <result name="appL" type="chain">applicantLeader</result> <result name="eng" type="chain">engineer</result> <result name="engL" type="chain">engineerLeader</result> </action> <action name="applicant" class="com.ehe.action.ApplicantAction"> <result name="JobPool">/WEB-INF/content/applicant/JobPool.jsp</result> <result name="HavePublish">/WEB-INF/content/applicant/HavePublish.jsp</result> <result name="HaveBeenUndertaken">/WEB-INF/content/applicant/HaveBeenUndertaken.jsp</result> <result name="HaveBeenFinished">/WEB-INF/content/applicant/HaveBeenFinished.jsp</result> <result name="operationSuccess">/WEB-INF/content/applicant/operationSuccess.jsp</result> </action> <action name="applicantLeader" class="com.ehe.action.ApplicantLeaderAction"> <result name="JobPool">/WEB-INF/content/applicantLeader/JobPool.jsp</result> <result name="NeedToApprove">/WEB-INF/content/applicantLeader/NeedToApprove.jsp</result> <result name="HaveApproved">/WEB-INF/content/applicantLeader/HaveApproved.jsp</result> <result name="JobAudit">/WEB-INF/content/applicantLeader/JobAudit.jsp</result> <result name="AuditJump">/WEB-INF/content/applicantLeader/AuditDetail.jsp</result> <result name="operationSuccess">/WEB-INF/content/applicantLeader/operationSuccess.jsp</result> </action> <action name="engineer" class="com.ehe.action.EngineerAction"> <result name="JobPool">/WEB-INF/content/engineer/JobPool.jsp</result> <result name="HaveUndertaken">/WEB-INF/content/engineer/HaveUndertaken.jsp</result> <result name="BeExecuting">/WEB-INF/content/engineer/BeExecuting.jsp</result> <result name="HaveFinished">/WEB-INF/content/engineer/HaveFinished.jsp</result> <result name="operationSuccess">/WEB-INF/content/engineer/operationSuccess.jsp</result> </action> <action name="engineerLeader" class="com.ehe.action.EngineerLeaderAction"> <result name="JobPool">/WEB-INF/content/engineerLeader/JobPool.jsp</result> <result name="NeedToFirstTrial">/WEB-INF/content/engineerLeader/NeedToFirstTrial.jsp</result> <result name="NeedToSecondTrial">/WEB-INF/content/engineerLeader/NeedToSecondTrial.jsp</result> <result name="JobAudit">/WEB-INF/content/engineerLeader/JobAudit.jsp</result> <result name="AuditJump">/WEB-INF/content/engineerLeader/AuditDetail.jsp</result> <result name="operationSuccess">/WEB-INF/content/engineerLeader/operationSuccess.jsp</result> </action> <action name="exit"> <result>/WEB-INF/content/login.jsp</result> </action> <action name="sub-*-*"> <result>/WEB-INF/content/{1}/{2}.jsp</result> </action> <action name="eq-*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
以上是关于struts2+spring+hibernate web项目参考示例的主要内容,如果未能解决你的问题,请参考以下文章
Struts2+Spring+Hibernate(SSH)框架的搭建
struts2集成Spring,Hibernate的问题!!
Spring:Spring整合Hibernate,之后整合Struts2