首页 > 代码库 > struts2 一个CRUD的BaseAction
struts2 一个CRUD的BaseAction
在struts2 in action中所见,这样封装后省去了大部分crud重复代码,虽然还不能理悟,先记下来。
abstract class BaseAction extends ActionSupport { protected DB db; protected static Log log = LogFactory.getLog(BaseAction.class); private long requestId; private boolean readOnly = false; private String mappedRequest; @SkipValidation public String show(){ setReadOnly(true); setMappedRequest(Constants.LIST); return SUCCESS; } @SkipValidation public String add(){ setMappedRequest(Constants.SAVE); return SUCCESS; } public String save(){ db.save(getModel()); return list(); } @SkipValidation public String edit(){ setMappingRequest(Constants.UPDATE); return SUCCESS; } public String update(){ db.save(getModel()); return list(); } @SkipValidation public String destroy(){ setReadOnly(true); setMappingRequest(Constants.REMOVE); return Constants.SUCCESS; } public String remove(){ db.remove(getModel()); return list(); } @SkipValidation public String list(){ setMappingedRequest(Constants.LIST); return Constants.LIST; } public String getActionClass(){ return getClass().getSimpleName(); } public String getDestination(){ return getClass().getSimpleName(); } public String getActionMethod(){ return mappedRequest; } public void setActionMethod(String method){ this.mappedRequest = method; } public void setMappedRequest(String actionMethed){ this.mappedRequest = getActionClass() + "_" + actionMethed; log.debug("setting mappedRequest to "+ getActionClass() + "_" + actionMethed); } public void setReadOnly(boolean readOnly){ this.readOnly = readOnly; log.debug("setting readOnly to "+readOnly); } public long getRequestId(){ return requestId; } public void setRequestId(long requestId){ this.requestId = requestId; } public void setDb(DB db){ this.db = db; } public boolean isReadOnly(){ return readOnly; } public abstract Object getModel(); }
public class MemberAction extends BaseAction implements ModelDriven,Preparable { private Member model; public Member getModel(){ return model; } public void prepare() throws Exception{ if(getRequestId() == 0){ model = new Member(); }else{ model = (Member) db.get(getRequestId); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。