Struts2中有一个很牛逼的action通配符,可以用来简化action配置,以我们将要讲解的案例来说,如果我们要对一个学生信息进行增加,删除,修改,那么按照原来的做法,我们需要写3个Action来配置:student_add,student_update,student_del。那么我们容易发现它们可以提取共同的前缀student_,这样我们就可以配置下通配符,把后面的东西弄成变量,那么我们就只要配置一个action就可以了。
1.首先编写我们的Action类
package com.babybus.sdteam.action; import java.sql.SQLException; import java.util.List; import com.babybus.sdteam.dao.StudentHibernateDao; import com.babybus.sdteam.vo.Student; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class ManageStudentAction extends ActionSupport implements Action { private static final long serialVersionUID = 1L; private String studentname; private Integer age; private String classname; private Integer userid; private String username; private List<Student> resultList; public String getStudentname() { return studentname; } public void setStudentname(String studentname) { this.studentname = studentname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public List<Student> getResultList() { return resultList; } public void setResultList(List<Student> resultList) { this.resultList = resultList; } /** * 获取条件 * @param method * @return */ private Student getCondition(String method) { Student result = new Student(); if(!"del".equals(method)) { result.setStudentname(this.studentname); result.setAge(this.age); result.setClassname(this.classname); } if("update".equals(method)) { result.setId(this.userid); } return result; } /** * 增加信息 * @throws SQLException */ public String studentadd() throws SQLException { Student condition = getCondition("add"); StudentHibernateDao dao = new StudentHibernateDao(); dao.insertStudent(condition); // 查询结果集合 resultList = dao.queryStudent(null); return SUCCESS; } /** * 修改信息 * @throws SQLException */ public String studentupdate() throws SQLException { Student condition = getCondition("update"); StudentHibernateDao dao = new StudentHibernateDao(); dao.updateStudent(condition); // 查询结果集合 resultList = dao.queryStudent(null); return SUCCESS; } /** * 删除信息 * @throws SQLException */ public String studentdel() throws SQLException { StudentHibernateDao dao = new StudentHibernateDao(); dao.deleteStudent(this.userid); // 查询结果集合 resultList = dao.queryStudent(null); return SUCCESS; } }
2.配置我们的struts.xml
<action name="student_*" class="com.babybus.sdteam.action.ManageStudentAction" method="student{1}"> <result name="success">/UserList.jsp</result> </action>
3.前端调用
<form action="student_add.action" method="post" > <form action="student_update.action" method="post" > <a href="student_del.action?userid=${student.id}" onclick="return confirm('确定要删除么?')" }>删除</a></td>
4.这样我们就可以用通配符节省很多action的配置了
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4808043.html