There's a great action wildcard in Struts2 that can be used to simplify the action configuration, in the case we're going to explain, if we want to Add, delete, modify a student's information , then according to the original practice, we need to write 3 action to configure:Student_add,student_update,student_del. Then it is easy to find that they can extract the common prefix student_, so that we can configure the next wildcard character , the following things into variables, then we just configure an action to do.
1. First write our action class
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;privat e 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;} /** * Get Conditions * @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; /** * Add information * @throws SQLException */public String studentadd () throws sqlexception{student condition = getcondition ("Add" ); Studenthibernatedao dao = new Studenthibernatedao ();d ao.insertstudent (condition);//query result set resultlist = Dao.querystudent (null); return SUCCESS;} /** * Modify information * @throws SQLException */public String studentupdate () Throws sqlexception{student condition = getcondition ("Update"); Studenthibernatedao dao = new Studenthibernatedao ();d ao.updatestudent (condition);//query result set resultlist = Dao.querystudent (null); return SUCCESS;} /** * Delete Information * @throws SQLException */public String Studentdel () throws Sqlexception{studenthibernatedao dao = new Studenth Ibernatedao ();d ao.deletestudent (This.userid);//query result set resultlist = Dao.querystudent (null); return SUCCESS;}}
2. Configure our struts.xml
<action name= "student_*" class= "com.babybus.sdteam.action.ManageStudentAction" method= "Student{1}" > <result name= "Success" >/UserList.jsp</result>
3. front-end invocation
<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 (' OK to delete? ') '}> delete </a></td>
4. So that we can use wildcards to save a lot of action configuration
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4808043.html
[Javaweb Base] 018.struts2 action wildcard use