Struts2+Hibernate+Spring框架搭建(三)

三:搭建Spring 
1.添加Spring的Jar包 

 

2.添加Struts2,Spring插件的Jar包 

 

3.添加Oracle10g 数据库的Jar包 

 

4.使用Spring来管理struts2的Action,DAO层,Service层 
1)在原来web.xml基础之上添加Spring的监听器 
 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <filter>  
  11.      <filter-name>struts2</filter-name>  
  12.      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  13.   </filter>  
  14.     
  15.   <filter-mapping>  
  16.      <filter-name>struts2</filter-name>  
  17.      <url-pattern>/*</url-pattern>  
  18.   </filter-mapping>  
  19.     
  20.   <listener>  
  21.       <!--监听器在default情况下读取的是:/WEB-INF/applicationContext.xml -->  
  22.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  23.   </listener>  
  24.     
  25.   <context-param>  
  26.       <param-name>contextConfigLocation</param-name>  
  27.         <!--   
  28.         <param-value>  
  29.             /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml  
  30.         </param-value>  
  31.          -->  
  32.         <!-- 监听器启动后会读取classpath目录下面的beans.xml文件 -->  
  33.       <param-value>classpath:beans.xml</param-value>  
  34.   </context-param>  
  35.     
  36.     
  37. </web-app>  


2)修改loginAction的内容 
Java代码   收藏代码
  1. package com.wl.struts.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import com.wl.po.Student;  
  5. import com.wl.service.studentService;  
  6.   
  7. @SuppressWarnings("serial")  
  8. public class loginAction extends ActionSupport {  
  9.   
  10.     private studentService stuService;  
  11.       
  12.     public studentService getStuService() {  
  13.         return stuService;  
  14.     }  
  15.   
  16.     public void setStuService(studentService stuService) {  
  17.         this.stuService = stuService;  
  18.     }  
  19.   
  20.     @Override  
  21.     public String execute() throws Exception {  
  22.   
  23.         Student stu=new Student();  
  24.         stu.setId("10");  
  25.         stu.setStuName("Zhangsan");  
  26.         stu.setStuAge(29);  
  27.         stu.setStuGrade("A");  
  28.         stu.setStuNo("201017");  
  29.         stu.setStuSex("male");  
  30.         stuService.saveStudent(stu);  
  31.         return SUCCESS;  
  32.     }  
  33.   
  34. }  

3)在Src目录下新建bean.xml文件 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:contentx="http://www.springframework.org/schema/content"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/content  
  8.            http://www.springframework.org/schema/content/spring-content-2.5.xsd">  
  9.   
  10.   <!-- configure the sessionFactory -->  
  11.   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  12.     <property name="configLocation">  
  13.         <value>classpath:hibernate.cfg.xml</value>  
  14.     </property>  
  15.   </bean>  
  16.     
  17.   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  18.       <property name="sessionFactory" ref="sessionFactory"></property>  
  19.   </bean>  
  20.     
  21.   <!-- manager the dao -->  
  22.   <bean id="studentDao" class="com.wl.dao.impl.studentDaoImpl">  
  23.      <property name="sessionFactory" ref="sessionFactory"></property>  
  24.   </bean>  
  25.     
  26.   <!-- manager the service  -->  
  27.   <bean id="studentService" class="com.wl.service.impl.studentServiceImpl">  
  28.      <property name="stuDao" ref="studentDao"></property>  
  29.   </bean>  
  30.     
  31.   <!-- manager the action in struts2 -->  
  32.   <bean id="loginAction" class="com.wl.struts.action.loginAction" scope="prototype">  
  33.      <property name="stuService">  
  34.         <ref bean="studentService"/>  
  35.      </property>  
  36.   </bean>    
  37. </beans>  

4)这时部署项目到Tomcat,运行Tomcat,结果如下 

 
点击“test Action”操作,报500错 

 
说明未将studentService注入到loginAction中。 

A:这个问题的原因是: loginAction没有真正的纳入Spring的管理当中 
Spring的bean.xml配置文件的确对loginAction做了配置 
Java代码   收藏代码
  1. <!-- manager the action in struts2 -->  
  2. <bean id="loginAction" class="com.wl.struts.action.loginAction" scope="prototype">  
  3.    <property name="stuService">  
  4.       <ref bean="studentService"/>  
  5.    </property>  
  6. </bean>   

但是Struts.xml配置文件中 
Java代码   收藏代码
  1. <package name="login" namespace="/" extends="struts-default">  
  2.        <action name="login" class="com.wl.struts.action.loginAction">  
  3.            <result name="success">/success.jsp</result>  
  4.            <result name="error">/error.jsp</result>  
  5.        </action>  
  6.    </package>  

没有用到Spring管理的loginAction,而是用类名重新引用的。loginAction 
B:解决这个问题的方法: 
修改Struts.xml的配置信息: 
Java代码   收藏代码
  1. <package name="login" namespace="/" extends="struts-default">  
  2.        <action name="login" class="loginAction">  
  3.            <result name="success">/success.jsp</result>  
  4.            <result name="error">/error.jsp</result>  
  5.        </action>  
  6.    </package>  

将原来的class="com.wl.struts.action.loginAction" 替换为 class="loginAction",其中的loginAction就是Spring中定义的id为loginAction的bean。 
备注: 
Spring对Struts的Action管理配置中不能忘记  scope="prototype" 
默认情况下,Spring从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spring容器只存在一个共享的bean实例,默认的配置。 Prototype: 每次对bean的请求都会创建一个新的bean实例;二者选择的原则:有状态的bean都使用Prototype作用域,而对无状态的bean则应该使用singleton作用域。 
在ssh2 项目中,struts2的action交由spring管理的时候,spring默认是singleton的,而struts2的action显然是有状态的,所以必须显示设置为scope="prototype",prototype为原型模式,每次action请求过来都会创建一个action但是对那些Dao的实现类推介scope="singleton" ,因为这些类没有状态,用singleton只需维护一个实例,显然性能高一些。 

5)重新部署项目,启动Tomcat后,点击“test Action”连接后控制条结果如下: 


但是从数据库中查找数据,数据没有添加成功。 
A: 问题原因: 缺少对事务的管理,数据未持久化到数据库中 
B:解决的方法:添加对事务的管理,在bean.xml文件中添加配置信息 
Java代码   收藏代码
  1. <!-- Transaction configuration Information -->  
  2.  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  3.    <property name="sessionFactory" ref="sessionFactory"></property>  
  4. </bean>  
  5.   
  6. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  7.     <tx:attributes>  
  8.         <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />  
  9.     </tx:attributes>  
  10. </tx:advice>  
  11.   <!-- 执行com.wl.service包及其子包下面的任何类的以Student结尾的任何方法 -->  
  12.   <aop:config>  
  13.     <aop:advisor pointcut="execution(* com.wl.service..*.*Student(..))" advice-ref="txAdvice" />  
  14. </aop:config>  

现在重新启动Tomcat后,点击“test Action”连接后,就会将数据持久化到数据库中。
  • Spring_Jar.rar (4.4 MB)
  • 描述: 用到的Spring的Jar包
  • 下载次数: 106
  • FirstSshProject_001.rar (16.2 KB)
  • 描述: Struts2+Hibernate+Spring框架搭建的整个工程(不包含Jar包)
  • 下载次数: 75
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
更新历史3.0: 环境:jdk1.5 tomcat5.5.数据库用oracle.如果不用oracle的话.可能菜单表的查询出不来.我的菜单表的查询允许选择上级菜单查询该菜单所对应的所有下级菜单.所以用了递归查询. criteria.add(Restrictions.sqlRestriction("MENUITEM_ID in(select a.MENUITEM_ID from Wuxin_MENUITEM a connect by prior a.MENUITEM_ID = a.PARENT_ID"+ " start with a.MENUITEM_ID = '"+parentId+"')" )); mysql和sqlserver不知道有没有start with这个sql. jar包:项目中除了使用了struts2,hibernate3.0和spring1.8以外(用spring2.0的包也可以.不能低于1.8的包.)还是用了junit,ajax,第三方的table组件.等等.所以需要下载相对应的包. 为了上传jar.我专门申请了一个网盘.所有下载地址在下载下来的这个rar包里面都有详细的介绍和说明. 说一下这个rar里面带的东西.除了源码外带了几篇文档.分别是关于项目中所使用的dwr的配置.table组件的配置说明文档.junit单元测试说明文档.还有我写这个例子时应该注意的一些东西.外加一些关于struts2和hibernate的技巧心得整理. 说一下这次3.0更新的内容.可能大家在我的博客里面已经看到了相关的日志.我懒.下面就把那个日志抽出来当说明了啊.有兴趣的就下载下来瞅瞅.偶也是菜鸟的.写的不好不要骂街啊..在此感谢各位网友的期待和支持. 这几天我终于闲下来了.也有时间开始写struts2的第三个框架版本了.主要是针对写了第二个版本之后的一些问题做一些回答才做得第三个版本.中间加一些小技巧之类的.不过我觉得还是值得一些关注的朋友期待的. 如果没有意外.这个版本应该是一个定型的版本了.在这段期间.有很多朋友问的问题大部分其实都已经不是struts2的范围了.有些都是hibernatespring的.介于前两个版本都是单表.对hibernate的引用还是比较少的.这次索性写个多对多关系好了.打算写个权限系统好了.我就使用权限5张表.用户表,权限表.角色表.用户角色表和角色菜单表.(麻雀虽小...五脏俱全了啊...)当然这个写起来就费劲一些了.... 为了能够更好的使用各方面的技术.所以这次打算弄个大锅(弄个大锅也很累的.不过大家可能到时候配这个框架也就比较麻烦点了...不过我觉得还是能多学一下总是好的.) 说一下大锅的内容吧.自己写了个分页组件.(不是太好看...).现在ajax都已经不是什么新鲜东东了..我在里面配的是dwr..(这个简单些..其他的我看着晕...).没有自己写页面输出.我使用的table组件是:eXtremeComponents.自我感觉这个组件比较好.所以把这个组件配入进来了.随着这篇文章的发布.我会陆续在博客中更新相关针对与当前项目模块的关于struts2的一些知识点.当作大家一起进步了... 也希望各位高人多多指点啊... 这个版本起名为Struts2Test3.0.以后的日志相关内容就为关于Struts2Test3.0例子的相关内容.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值