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
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
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
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
map.put("gradeId", gradeId);
List
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
return getHibernateTemplate().loadAll(Student.class);
}
public List
List
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文件:
parameter="cmd"
name="studentForm"
scope="request">
parameter="cmd"
name="gradeForm"
scope="request">
applicationContext.xml配置文件:
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">
需要说明的是,由于spring dtd规定id不能有"/",所以我们用name定义path,并且,spring bean的name要和struts-config.xml中的path一致
使用DelegatingActionProxy的好处就在于你可以用不用任何spring特定的类编写Struts Action,这个方法也有不足之处,就是不太直观,因为所有路径都映射到同一个类了
对于这种情况,spring也有解决方法,就是使用请求委托
首先,为struts-config.xml增加controller
然后,修改我们的path定义位
这样,又和我们单独使用struts的时候一样了,但内部还是让spring取代理我们的真正的action
需要说明的是,这里的type其实是个摆设,完全可以使用