📄 abstractcrudaction.java
字号:
package com.easyjf.web.tools;
import java.lang.reflect.Method;
import com.easyjf.dbo.EasyJDB;
import com.easyjf.util.CommUtil;
import com.easyjf.web.IWebAction;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebForm;
/**
*
* <p>Title: 通过添删改查(CRUD)处理Action类</p>
* <p>Description: 处理普通数据表的添删改查(CRUD)处理的抽象类,用户只需继承该Action,并根据自身的情况实现其中的模板方法即可。</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: www.easyjf.com</p>
* @author 蔡世友
* @version 1.0
*/
public abstract class AbstractCrudAction implements ICommCRUDAction, IWebAction {
private String command;
private static final String systemCommand="new,edit,add,update,del,list,query,";
public Page execute(WebForm form, Module module)throws Exception {
String method=CommUtil.null2String(form.get("easyJWebCommand"));
this.command=method;
Object beforeCheck=doBefore(form,module); //模版方法
if(beforeCheck!=null)
{
//如果Action执行前检查,返回Page则直接跳转
if(beforeCheck.getClass()==Page.class)return (Page)beforeCheck;
}
IActiveUser user=(IActiveUser)getCurrentUser(form);
Page forward=null;
doInit(form,module);
if("new".equals(method))
{
forward=module.findPage("edit");
}
else if("edit".equals(method))
{
forward=doEdit(form,module,user);
}
else if("add".equals(method))
{
forward=doAdd(form,module,user);
}
else if("update".equals(method))
{
forward=doUpdate(form,module,user);
}
else if("del".equals(method))
{
forward=doDel(form,module,user);
}
else if("".equals(method)|| "list".equals(method)||"query".equals(method))
{
forward=doQuery(form,module,user);
}
else//其它命令直接调用doCommand方法
{
Class[] paras=new Class[2];
paras[0]=WebForm.class;
paras[1]=Module.class;
String cmd="do"+method.substring(0,1).toUpperCase()+method.substring(1);
Method m=this.getClass().getMethod(cmd,paras);
if(m!=null)
{
Object[] objs=new Object[2];
objs[0]=form;
objs[1]=module;
Object ret=m.invoke(this,objs);
if(ret instanceof Page)forward=(Page)ret;
}
else
throw new Exception("方法名称不正确,在"+this.getClass()+"中找不到"+cmd+"方法!请确认您页面中的easyJWebCommand参数值是否正确!");
}
//forward=doOtherAction(form,module);
doAfter(form,module);
return forward;
}
public Page doAdd(WebForm form, Module module, IActiveUser user) {
Page forward=null;
Object obj=form2Obj(form);
if(obj==null)
{
form.addResult("msg","无法创建要保存的对象,添加失败!");
return module.findPage("edit");
}
EasyJDB db=EasyJDB.getInstance();
if(db.add(obj))
{
form.addResult("msg","数据添加成功!");
forward=doQuery(form,module,user);
}
else
{
form.addResult("msg","数据添加失败");
forward=module.findPage("edit");
}
return forward;
}
public Page doUpdate(WebForm form, Module module, IActiveUser user) {
Page forward=null;
Object obj=form2Obj(form);
EasyJDB db=EasyJDB.getInstance();
if(db.update(obj))
{
form.addResult("msg","数据修改成功!");
forward=doQuery(form,module,user);
}
else
{
form.addResult("msg","数据修改失败");
forward=module.findPage("edit");
}
return forward;
}
public Page doEdit(WebForm form, Module module, IActiveUser user) {
Page forward=null;
Object obj=form2Obj(form);
if(obj!=null)
{
CommUtil.Obj2Map(obj,form.getTextElement());
forward=module.findPage("edit");
}
else
{
form.addResult("msg","找不到数据!");
forward=doQuery(form,module,user);
}
return forward;
}
public Page doDel(WebForm form, Module module, IActiveUser user) {
Object obj=form2Obj(form);
EasyJDB db=EasyJDB.getInstance();
if(db.del(obj))
{
form.addResult("msg","数据删除成功!");
}
else
{
form.addResult("msg","数据修改失败");
}
return doQuery(form,module,user);
}
public Page doQuery(WebForm form, Module module, IActiveUser user) {
int currentPage=CommUtil.null2Int(form.get("page"));
int pageSize=CommUtil.null2Int(form.get("pageSize"));
if(currentPage<1)currentPage=1;
if(pageSize<1)pageSize=15;
IPageList pList=doQuery(form,currentPage,pageSize);//调用模板方法执行数据查询
//保存查询结果
if(pList!=null){
form.addResult("list",pList.getResult());
form.addResult("pages",new Integer(pList.getPages()));
form.addResult("rows",new Integer(pList.getRowCount()));
form.addResult("page",new Integer(pList.getCurrentPage()));
form.addResult("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
}
return module.findPage("list");
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public boolean isSystemCommand()
{
boolean ret=false;
if("".equals(this.command)||systemCommand.indexOf(this.command)>=0)ret=true;
return ret;
}
public abstract IPageList doQuery(WebForm form,int currentPage,int pageSize);
public abstract void doInit(WebForm form,Module module);//初始化数据,如表单项及默认值等
public abstract IActiveUser getCurrentUser(WebForm form);//取得用户
public abstract Object doBefore(WebForm form,Module module);//Action前置校验
public abstract void doAfter(WebForm form,Module module);//Action后置处理
public abstract Object form2Obj(WebForm form);//处理form中的数据并返回相应的obj
//public abstract Page doOtherAction(WebForm form,Module module);//基本添删改查以外的Action
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -