java strut2通配符_[JavaWeb基础] 018.Struts2 Action通配符使用

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 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 getResultList() {

return resultList;

}

public void setResultList(List 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

/UserList.jsp

3.前端调用

删除

4.这样我们就可以用通配符节省很多action的配置了

ed5669605c297dd6c8b3c52608032c5a.png

本站文章为宝宝巴士)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值