spring整合struts2及Hibenrate
1、依次引入strut2、spring、hibernate组件包
2、生成Hibernate的实体类和映射文件以及DAO类
3、编写业务类和动作类(Action类)
4、在spring配置文件中注入业务类和Action类
5、applicationContext.xml中配置事务处理以及strutr.xml文件配置
6、配置web.xml文件,配置spring容器的启动
7、编写jsp页面进行测试
具体配置如下:
在编写applicationContext.xml文件时,要注意各个bean之间的引用关系
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/empdb">
</property>
<property name="username" value="root"></property>
<property name="password" value="java"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/Dep.hbm.xml</value>
<value>com/po/Users.hbm.xml</value>
<value>com/po/Welfare.hbm.xml</value>
<value>com/po/Empwelfare.hbm.xml</value>
<value>com/po/Salary.hbm.xml</value>
<value>com/po/Emp.hbm.xml</value></list>
</property></bean>
<bean id="DepDAO" class="com.dao.DepDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UsersDAO" class="com.dao.UsersDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="WelfareDAO" class="com.dao.WelfareDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="EmpwelfareDAO" class="com.dao.EmpwelfareDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="SalaryDAO" class="com.dao.SalaryDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="EmpDAO" class="com.dao.EmpDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 注入DaoService -->
<bean id="daos" class="com.service.DaoService">
<property name="depDAO" ref="DepDAO"></property>
<property name="empDAO" ref="EmpDAO"></property>
<property name="empwelfareDAO" ref="EmpwelfareDAO"></property>
<property name="salaryDAO" ref="SalaryDAO"></property>
<property name="usersDAO" ref="UsersDAO"></property>
<property name="welfareDAO" ref="WelfareDAO"></property>
</bean>
<!-- 注入biz -->
<bean id="depbiz" class="com.biz.imp.DepBiz">
<property name="daoService" ref="daos"></property>
</bean>
<bean id="empbiz" class="com.biz.imp.EmpBiz">
<property name="daoService" ref="daos"></property>
</bean>
<bean id="usersbiz" class="com.biz.imp.UsersBiz">
<property name="daoService" ref="daos"></property>
</bean>
<bean id="welfarebiz" class="com.biz.imp.WelfareBiz">
<property name="daoService" ref="daos"></property>
</bean>
<!-- 注入bizService -->
<bean id="bizs" class="com.service.BizService">
<property name="depBiz" ref="depbiz"></property>
<property name="empBiz" ref="empbiz"></property>
<property name="usersBiz" ref="usersbiz"></property>
<property name="welfareBiz" ref="welfarebiz"></property>
</bean>
<!-- 注入Action类 -->
<bean id="EmpAction" class="com.action.EmpAction">
<property name="bizService" ref="bizs"></property>
</bean>
<bean id="UsersAction" class="com.action.UsersAction">
<property name="bizService" ref="bizs"></property>
</bean>
<!-- 注入Hibernate的事务管理器 -->
<bean id="txmanager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注入事务通知属性 -->
<tx:advice id="txavd" transaction-manager="txmanager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="NOT_SUPPORTED"/>
</tx:attributes>
</tx:advice>
<!-- 注入事务的切入点 -->
<aop:config>
<aop:pointcut expression="execution(* com.biz.*.* (..))" id="bizpoint"/>
<aop:advisor advice-ref="txavd" pointcut-ref="bizpoint"/>
</aop:config>
</beans>
Struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="student" namespace="/" extends="struts-default">
<!-- {2}Action表示spring中配置的Action的id值 -->
<action name="*_*" class="{2}Action" method="{1}">
<result name="ok" type="redirect">${path}</result>
<result name="fail" type="redirect">fail.jsp</result>
</action>
</package>
</struts>
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<!-- ******************spring容器的启动配置,替代ApplicationContext对象的创建************* -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ***********************spring配置结束************************** -->
<!-- ************************配置Hiberante的session已关闭异常的过滤处理******************** -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ************************************Hiberante过滤处理结束************************** -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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></web-app>
最后,一般我们会在src目录下新建一个log4j.properties的日志文件,内容如下(此文件可帮助我们在控制台打印错误信息):
log4j.properties日志文件
日志文件可以帮我们查看错误,如果没有日志文件,出错了我们也看不见哦!
#All level less than INFO will be logged
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96893.html