您的位置:首页 > 产品设计 > 产品经理

04传智_jbpm与OA项目_部门模块改进_直接在BaseAction中实现ModelDriven<T>

2016-07-23 23:20 465 查看
这个项目是用Struts2做的,我这里单独写了一个BaseAction,用来存放所有的功能模块的Action的公共部分,

刚开始的做法是这个BaseAction只会继承ActionSupport 并不会实现ModelDriven<T>,而是让每一个具体的Action(比如DepartmentAction)去继承ModelDriven<T>,

但是为了实现代码不重复写,进行了改进。这里直接让BaseAction继承ModelDriven<T>,然后让所有的功能模块的Action都继承这个BaseAction,不用再继承ModeDriven<T>了。

BaseAction的代码如下:

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
private Class classt;
private T t;

public BaseActiom() {
// 这个构造函数的设计和之前的泛型Dao扩展是一样的,BaseAction是要用来被别的Action来继承的,所以这个就可以得到BaseAction中的泛型的类类型。
try {
ParameterizedType type = (ParameterizedType) this.getClass()
.getGenericSuperclass();
this.classt = (Class) type.getActualTypeArguments()[0];
// 直接创建一个T对象,比如如果传入的是Department这里就会创建一个Department
this.t = (T) this.classt.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static final String LISTACTION = "listAction";
public static final String ADDUI = "addUI";
public static final String UPDATEUI = "updateUI";
public static final String ACTION2ACTION = "action2ation";
// 返回这个String 就会跳转到新的界面  用来展示所有的部门
public String listAction = LISTACTION;
// 返回这个String 就会跳转到新的界面  用来增加一个部门
public String addUI = ADDUI;
// 返回这个String 就会跳转到新的界面  用来修改一个部门
public String updateUI = UPDATEUI;
// 一个Action跳到另外一个Action
public String action2action = ACTION2ACTION;

public T getModel() {

return t;
}

}


相对应的Struts2配置文件的写法:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="department" namespace="/" extends="struts-default">
<action name="DepartmentAction_*" method="{1}" class="DepartmentAction">

<result name="listAction">WEB-INF/jsp/department/listJsp.jsp</result>
<result name="action2ation" type="redirectAction">DepartmentAction_getAllDepartment.action </result>
<result name="addUI" >WEB-INF/jsp/department/add.jsp </result>
<result name="updateUI" >WEB-INF/jsp/department/update.jsp </result>
</action>
</package>
</struts>


每一个Action,比如DepartmetnAction 都是继承BaseAction ,和 implement ModeDriven<T>。ModeDrivern<T>是Action中传递参数的一中方式。

具体的DepartmentAction写法如下:

public class DepartmentAction extends BaseActiom<Department> {
/**
*
*/
//private static final long serialVersionUID = 7677215666794215686L;
//模型驱动的作用把数据放到栈顶
/**
* Struts2里面的模型驱的作用是 能够获取页面的数据
* 但是我们在保存这个新加的部门时不能直接就是add this.getMode。这样做
*/
//private Department model=new Department();
private DepartmentServer departmentServer;

//get方法
public DepartmentServer getDepartmentServer() {
return departmentServer;
}

//set方法
public void setDepartmentServer(DepartmentServer departmentServer) {
this.departmentServer = departmentServer;
}

public String getAllDepartment()
{
Collection<Department> departmentList=this.departmentServer.getAllDepartment();
//把departments放入map棧中
ActionContext.getContext().put("departmentList", departmentList);
return listAction;
}

/* (non-Javadoc)
* @see com.opensymphony.xwork2.ModelDriven#getModel()
*/
/*public Department getModel() {
return this.model;
}*/
public String deleteDepartment()
{
this.departmentServer.deleteDepartment(this.getModel().getDid(), DeleteDemo.DEL_PRE_RELEASE);
return action2action;
}
//跳转到添加部门的界面
public String  addUI()
{
return addUI;
}
//添加一个部门
public String add()
{
/**
* 我们不能直接做    this.departmentServer.saveDepartment(this.getModel());
*/

//应该这么做的
/**
* 1.先建立一个department
* 2.把模型驱动中的值赋值到department中
* 3.执行save方法保存
*
*/
Department department=new Department();
//就这么一行代码就值就过来了,对象的属性的赋值工程
BeanUtils.copyProperties(this.getModel(), department);
departmentServer.saveDepartment(department);
return action2action;
}
public String  UpdateUI()
{   //数据回显,把数据放在堆栈上
Department department=this.departmentServer.getDepartment(this.getModel().getDid());
//将对象放入栈顶
ActionContext.getContext().getValueStack().getRoot().add(0, department);
return updateUI;
}
public String Update()
{   /**
1.先根据Id把Department把值从数据库取出来。
2.把修改以后的数据赋值到该对象中
3,针对该对象进行Update操作
**/
//从数据库中根据id得到数据
Department department=this.departmentServer.getDepartment(this.getModel().getDid());
//从驱动中把最新的数据得到赋值给department
BeanUtils.copyProperties(this.getModel(), department);
//进行数据库的更新操作。
departmentServer.updateDepartment(department);
//跳转回查询到的界面。
return action2action;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: