Java Web SSH-Spring Hibernate Struts SSH三大框架整合概述
来源:互联网 发布:再生聚酯切片进口数据 编辑:程序博客网 时间:2024/06/05 14:45
Spring Hibernate Struts SSH三大框架
整合步骤
一、 整合顺序
1) Spring(管理组件间的依赖关系和事务管理)
2) Hibernate(数据持久化)
3) Struts2(交互)
SSH整合步骤概览:
以Spring为核心(粘合剂),数据持久化用Hibernate框架,表示层用Struts 2框架。
二、 分步整合
Spring和Hibernate整合
配置Spring:
1) 导入Spring 3.1 Core Libraries包(Spring核心包);
2) 导入Spring 3.1 Persistence Libraries包(Spring Framework提供的数据持久化支持);
配置Spring配置文件:
1) 启用AOP Builder,并在src下创建applicationContext.xml配置文件,点击完成即可
配置Hibernate:
1) 导入Hibernate 3.3 Core Libraries包(Hibernate核心包);
2) 导入Hibernate 3.3 Advanced Support Libraries包;
配置Hibernate配置文件:
1) 定义Hibernate配置时使用Spring的配置文件applicationContext.xml;
2) 定义Spring-Hibernate配置时使用已存在的applicationContext.xml,且Spring Config为“src/applicationContext.xml”;
3) 选择好登录到数据库的用户;
4) 不勾选创建Session工厂,使用Spring容器对其进行管理,点击完成即可。
Spring和Struts整合
配置Struts:
1) 使用Struts 2框架的2.1版本,选用.action为过滤器过滤的后缀;
2) 导入Struts 2 Core Libraries核心包;
3) 导入Struts 2 Spring Libraries包(Struts 2 Framework提供对SpringFramework的支持),点击完成即可。
较标准的项目结构一览:
三、 注意事项
1. 在接口的实现类中,原有书写SQL语句的风格有所不同:
示例代码:
/** * 添加报销单的方法 */ @Override public Booleanadd(BizClaimVoucher claimVoucher) { Booleanresult = false; try { super.getHibernateTemplate().save(claimVoucher); result= true; } catch(Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } returnresult; }
需要让接口的实现类继承HibernateDaoSupport类,使用此父类的方法。
2. 在业务层的实现类中,定义相关DAO类和实例化的方式有所不同:
示例代码:
/** * 报销单接口属性 */ private ClaimVoucherDao voucherDao; /** * 报销单详情接口属性 */ private ClaimVoucherDetailDaovoucherDetailDao; public ClaimVoucherDetailDaogetVoucherDetailDao() { return voucherDetailDao; } public voidsetVoucherDetailDao(ClaimVoucherDetailDao voucherDetailDao) { this.voucherDetailDao =voucherDetailDao; } public ClaimVoucherDao getVoucherDao(){ return voucherDao; } public voidsetVoucherDao(ClaimVoucherDao voucherDao) { this.voucherDao = voucherDao; } /** * 保存报销单的方法【业务逻辑】 * * @param claimVoucher * 报销单主要信息实体 * @param itemTypes * 项目类别集合 * @param itemMoneys * 项目金额集合 * @param itemDescriptions * 项目说明集合 * @return */ @Override public BooleansaveClaimVoucher(BizClaimVoucher claimVoucher, String[] itemTypes,String[] itemMoneys, String[] itemDescriptions) { Boolean main, secondary =null; // 保存报销单主要信息 main = voucherDao.add(claimVoucher); // 保存报销单详细信息 for (int i = 0; i <itemTypes.length; i++) { BizClaimVoucherDetailclaimVoucherDetail = new BizClaimVoucherDetail(); claimVoucherDetail.setAccount(Double.parseDouble(itemMoneys[i])); claimVoucherDetail.setDes(itemDescriptions[i]); claimVoucherDetail.setItem(itemTypes[i]); claimVoucherDetail.setBizClaimVoucher(claimVoucher); secondary =voucherDetailDao.add(claimVoucherDetail); } // 全部为true则表明报销单表和报销单详情表都成功插入数据,返回true if (main == true && secondary== true) { return true; } else { return false; } }
要在业务实现类中定义DAO类接口类型的属性,并封装其getter/setter方法,但不实例化。
3. 在Action中定义相关Biz的DAO类和实例化的方式有所不同:
示例代码:
/** * */ private static final longserialVersionUID = -1533310365193789445L; /** * 员工业务接口属性 */ private EmployeeBiz employeeBiz; /** * 用户名属性 */ private String username; /** * 用户密码属性 */ private String password; public String getUsername() { return username; } public void setUsername(Stringusername) { this.username = username; } public String getPassword() { return password; } public void setPassword(Stringpassword) { this.password = password; } public EmployeeBiz getEmployeeBiz() { return employeeBiz; } public void setEmployeeBiz(EmployeeBiz employeeBiz){ this.employeeBiz =employeeBiz; } /** * 默认执行的方法 */ @Override public String execute() throwsException { Boolean resu =employeeBiz.IsLogin(username, password); if (resu == true) { returnEnumResultType.SUCCESS.getTrueType(); } else { returnEnumResultType.FAILED.getTrueType(); } }
需要在Action中定义业务接口类型的属性,并封装其getter/setter方法,但不实例化。
4. 在配置applicationContext.xml配置文件时,遵循从“小接口到大接口”的配置顺序逐一配置以避免漏配。
5. 如果在applicationContext.xml配置文件中配置了Action,则在struts.xml配置文件中,Action的name属性值和class属性值都要和applicationContext.xml配置文件中配置的Action的Bean的id一致。
6. JSP页面form表单提交的位置即在struts.xml配置文件中配置的Action的name属性值+”.action”,与之前的方式无异。
7. 注意:不要忘记配置web.xml文件!
示例代码:
<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>
注意:要在<web-app>节点下配置。
四、 附
1. 完整applicationContext.xml配置文件示例:
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd "> <!--******************************************************************* --> <!--配置员工DAO --> <beanid="EmpDao" class="com.practice1.extend.dao.impl.EmployeeDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <!--配置员工BIZ --> <beanid="EmpBiz"class="com.practice1.extend.biz.impl.EmployeeBizImpl"> <propertyname="employeeDao" ref="EmpDao"></property> </bean> <!--配置员工登录的ACTION --> <beanid="EmpLogin"class="com.practice1.extend.action.EmpLoginAction"> <propertyname="employeeBiz" ref="EmpBiz"></property> </bean> <!--******************************************************************* --> <!--配置报销单DAO --> <beanid="CVDao"class="com.practice1.extend.dao.impl.ClaimVoucherDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <!--配置报销单BIZ --> <beanid="CVBiz"class="com.practice1.extend.biz.impl.ClaimVoucherBizImpl"> <propertyname="voucherDao" ref="CVDao"></property> <propertyname="voucherDetailDao" ref="CVDDao"></property> </bean> <!--配置报销单详情DAO --> <beanid="CVDDao" class="com.practice1.extend.dao.impl.ClaimVoucherDetailDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <!--配置加载报销单主页的ACTION --> <beanid="loadCVMainPage"class="com.practice1.extend.action.LoadClaimVoucherIndexAction"> <propertyname="voucherBiz" ref="CVBiz"></property> </bean> <!--配置处理添加报销单信息的ACTION --> <beanid="execCV"class="com.practice1.extend.action.AddNewClaimVoucherAction"> <!--报销单接口接口属性和报销单详情接口属性共用一个业务,所以暂时不必配置报销单详情的业务类 --> <propertyname="voucherBiz" ref="CVBiz"></property> <!--在添加报销单信息业务中还用到了员工相关的接口,所以还要引用员工业务接口用来注入实例 --> <propertyname="employeeBiz" ref="EmpBiz"></property> </bean> <!--定义事务管理器 --> <beanid="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <!--******************************************************************* --> <!--定义事务规则 --> <tx:adviceid="txAdv" transaction-manager="txManager"> <tx:attributes> <tx:methodname="*Login" read-only="true" /> <tx:methodname="add" propagation="REQUIRED" /> <tx:methodname="save*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!--定义切面和切入点并织入切面 --> <aop:config> <aop:pointcutexpression="execution(* com.practice1.extend.biz.*.*(..))" id="pointcut"/> <aop:advisoradvice-ref="txAdv" pointcut-ref="pointcut" /> </aop:config> <!--******************************************************************* --> <beanid="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"> </property> <propertyname="url"value="jdbc:oracle:thin:@localhost:1521:orcl"> </property> <propertyname="username" value="scott"></property> <propertyname="password" value="admin"></property> </bean> <beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <propertyname="dataSource"> <refbean="dataSource" /> </property> <propertyname="hibernateProperties"> <props> <propkey="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> </props> </property> <propertyname="mappingResources"> <list> <value> com/practice1/extend/entity/BizCheckResult.hbm.xml </value> <value> com/practice1/extend/entity/SysPosition.hbm.xml </value> <value> com/practice1/extend/entity/SysEmployee.hbm.xml </value> <value> com/practice1/extend/entity/BizClaimVoucher.hbm.xml </value> <value> com/practice1/extend/entity/SysDepartment.hbm.xml </value> <value> com/practice1/extend/entity/BizClaimVoucherDetail.hbm.xml </value> </list> </property> </bean></beans>
完整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"> <display-name></display-name> <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> <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>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> </web-app>
- Java Web SSH-Spring Hibernate Struts SSH三大框架整合概述
- 三大框架的整合技术 SSH spring,hibernate,struts;
- Struts+Spring+Hibernate三大框架(SSH)怎样整合
- ssh(Struts+spring+Hibernate)三大框架整合-简述
- SSH Java三大框架整合(Struts 2+Hibernate+Spring+mysql)
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)
- SSH三大框架整合(Hibernate,Spring,struts2)
- Struts2,Spring,Hibernate三大框架的整合(SSH)
- Spring+SpringMVC+Hibernate三大框架整合的SSH
- Spring+Struts2+Hibernate(ssh)三大框架整合流程
- SSH(spring、struts2、hibernate)三大框架整合
- Sorts Colors
- MySQL索引及优化
- 郁闷的gxg
- Android的Handler、MessageQueue、Runnable和Looper的联系(源码选自21版本)
- 路径规划算法初探
- Java Web SSH-Spring Hibernate Struts SSH三大框架整合概述
- (转载)Deep Learning(深度学习)学习笔记整理系列之(八)
- Android官方文档翻译 二 1.Building Your First App
- 正则表达式一
- 建造者模式Builder Pattern
- Norton Internet Security 序列号提取(卡饭)
- 自定义view
- Android连接Tomcat服务器
- uploadifyJcrop