spring 整合 struts1.x

spring和struts1.x的整合有三种方法:本人习惯使用其中的一种,在这里做一下简要的介绍,如果对其他整合方法感兴趣的话,可以查阅相关资料。

低耦合的Struts集成Spring的实例 (以简单的学生管理系统为例)

我们在集成Spring和struts的时候,往往习惯于使用spring提供的ActionSupport,然后使用getWebApplicationContext()方法获得spring的bean,这样固然方便,但有一个弊端,就是我们的struts action依赖了spring的api,增加了耦合,现在什么都流行高内聚,低耦合,spring为我们提供了代理的Struts action,这样,我们在struts-config.xml不再为path设置真正的action,而是设计spring的代理Action,然后由spring的代理action,去寻找在spring bean 容器中的真正的action,这样,我们的action是一个完全没有依赖于spring的action ,具体实现请看以下代码:

public class StudentAction extends DispatchAction {

 private StudentDao studentDao;
 private GradeDao gradeDao;
   
 public StudentDao getStudentDao() {
  return studentDao;
 }

 public void setStudentDao(StudentDao studentDao) {
  this.studentDao = studentDao;
 }
  
 public GradeDao getGradeDao() {
  return gradeDao;
 }

 public void setGradeDao(GradeDao gradeDao) {
  this.gradeDao = gradeDao;
 }

 public ActionForward loadadd(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
     List<Grade> list = gradeDao.list();
     request.setAttribute("list", list);
  return mapping.findForward("loadadd");
 }
 
 public ActionForward add(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  StudentForm sf = (StudentForm)form;
  Student stu = sf.getStu();
  
  String gradeId = request.getParameter("gradeId");
  Grade grade = new Grade();
  grade.setGradeId(Integer.parseInt(gradeId));
  
  stu.setGrade(grade);
  
  studentDao.add(stu);
  return mapping.findForward("add");
 }
 
 public ActionForward loadedit(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  String id = request.getParameter("id");
  Student stu = studentDao.getStu(Integer.parseInt(id));
  request.setAttribute("stu", stu);
  List<Grade> list = gradeDao.list();
     request.setAttribute("list", list);
  return mapping.findForward("loadedit");
 }
 
 public ActionForward edit(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  StudentForm sf = (StudentForm)form;
  Student stu = sf.getStu();
  
  String gradeId = request.getParameter("gradeId");
  Grade grade = new Grade();
  grade.setGradeId(Integer.parseInt(gradeId));
  
  stu.setGrade(grade);
  
  studentDao.edit(stu);
  return mapping.findForward("edit");
 }
 
 public ActionForward delete(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  String id = request.getParameter("id");
  studentDao.delete(Integer.parseInt(id));
  return mapping.findForward("delete");
 }
 
 public ActionForward list(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  List<Student> list = studentDao.list();
  request.setAttribute("list", list);
   return mapping.findForward("list");
 }
 
 public ActionForward search(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  String gradeId= request.getParameter("gradeId");
  Map<String, String> map = new HashMap<String, String>();
  map.put("gradeId", gradeId);
  List<Student> list  = studentDao.search(map);
  request.setAttribute("list", list);
  return mapping.findForward("search");
 } 
}

 

Dao接口的实现类:

public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao {

 public void add(Student stu) {
  getHibernateTemplate().save(stu);
 }

 public void delete(int id) {
  getHibernateTemplate().delete(
    getHibernateTemplate().get(Student.class, id));
 }

 public void edit(Student stu) {
  getHibernateTemplate().update(stu);
 }

 public Student getStu(int id) {
  return (Student) getHibernateTemplate().get(Student.class, id);
 }

 public List<Student> list() {
  return getHibernateTemplate().loadAll(Student.class);
 }

 public List<Student> search(Map<String, String> map) {
  List<Student> list = null;
  String gradeId = map.get("gradeId");
  String hql = "from Student where 1=1 ";
  
  if (gradeId != null && !"".equals(gradeId)) {
   hql += " and gradeId=" + gradeId;
  }
  list =  getHibernateTemplate().find(hql);
  return list;
 }

}

现在书写struts-config.xml文件:

<struts-config>
  <data-sources />
  <form-beans>
      <form-bean name="studentForm" type="com.ssh.form.StudentForm" />
      <form-bean name="gradeForm" type="com.ssh.form.GradeForm" />
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
       <action path="/student"
               type="org.springframework.web.struts.DelegatingActionProxy"
               parameter="cmd"
               name="studentForm"
               scope="request">
            <forward name="loadadd" path="/student_add.jsp" />
            <forward name="add" path="/student.do?cmd=list" redirect="true" />
            <forward name="loadedit" path="/student_edit.jsp" />
            <forward name="edit" path="/student.do?cmd=list" redirect="true" />
            <forward name="delete" path="/student.do?cmd=list" redirect="true" />
            <forward name="list" path="/student_list.jsp" />
            <forward name="search" path="/student_list.jsp" />
           
       </action>
       <action path="/grade"
               type="org.springframework.web.struts.DelegatingActionProxy"
               parameter="cmd"
               name="gradeForm"
               scope="request">
            <forward name="loadadd" path="/grade_add.jsp" />
            <forward name="add" path="/grade.do?cmd=list" redirect="true" />
            <forward name="loadedit" path="/grade_edit.jsp" />
            <forward name="edit" path="/grade.do?cmd=list" redirect="true" />
            <forward name="delete" path="/grade.do?cmd=list" redirect="true" />
            <forward name="list" path="/grade_list.jsp" />
            <forward name="search" path="/grade_list.jsp" />
           
       </action>
  </action-mappings>
  <message-resources parameter="ApplicationResources" />
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
     <set-property property="contextConfigLocation" value="classpath:applicationContext-*.xml"/>
  </plug-in>
</struts-config>

 

 

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"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
<!-- 声明dao的实现类 -->
 <bean id="studentDao" class="com.ssh.dao.impl.StudentDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <bean id="gradeDao" class="com.ssh.dao.impl.GradeDaoImpl">
      <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <!-- 与struts的整合 --> 
 <bean name="/student" class="com.ssh.action.StudentAction">
   <property name="studentDao" ref="studentDao" />
   <property name="gradeDao" ref="gradeDaoProxy" />
 </bean>
 <bean name="/grade" class="com.ssh.action.GradeAction">
   <property name="gradeDao" ref="gradeDao" />
 </bean>
</beans>

需要说明的是,由于spring dtd规定id不能有"/",所以我们用name定义path,并且,spring bean的name要和struts-config.xml中的path一致

使用DelegatingActionProxy的好处就在于你可以用不用任何spring特定的类编写Struts Action,这个方法也有不足之处,就是不太直观,因为所有路径都映射到同一个类了

对于这种情况,spring也有解决方法,就是使用请求委托

首先,为struts-config.xml增加controller

  <!-- 使用请求委托 -->
 <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
 </controller>
然后,修改我们的path定义位 <action path="/listStudentAction" type="action.ListStudentActionAction"/>

这样,又和我们单独使用struts的时候一样了,但内部还是让spring取代理我们的真正的action

需要说明的是,这里的type其实是个摆设,完全可以使用 <action path="/listStudentAction"/>,写上是为了解决我们上面提到的“不够直观的”的问题


本文出自 “樵夫的扁担” 博客,转载请与作者联系!

你可能感兴趣的:(spring,action,public,流行,管理系统)