• 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);
    		}
    	}
    }


  • 相关阅读:
    【二分匹配】【匈牙利算法即由增广路求最大匹配模板】
    【字符串入门专题1】【hdu4763】【未ac题】【扩展kmp exkmp】
    【字符串入门专题1】hdu3613 【一个悲伤的exkmp】
    【如何用XAMPP搭建Wordpress建站环境】
    面向过程要素
    当前阶段--可确定--完善版--学习结构--2016.09.10
    什么时候用工厂模式-----转载
    《EffectiveJava中文版》
    《ARM LINUX内核源码解析》
    《深入分析javaweb技术内幕》
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7119613.html
Copyright © 2020-2023  润新知