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"/>,寫上是爲了解決我們上面提到的“不夠直觀的”的問題


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章