您的位置:首页 > 编程语言 > Java开发

用泛型写Struts2的BaseAction出错

2008-03-22 22:48 381 查看
想写一个用于Struts2的Action基础类BaseAction,里面有些常用的CRUD方法,可是,不知怎么回事,从后台到前台的转换可以顺利进行,但是要save前台的entity到数据库的时候,泛型总是把前台Struts2传来的对象作为Object处理,从而报错。

我得BaseAction如下:




/** *//**


* @(#)cn.pingyutech.base.action.BaseAction.java


* @author <a href="mailto:newweapon111@gmail.com">Liu Weiping</p>


* @version 1.0


* @date 2008-3-22


*


* Copyright 2008 PingYuTech, Inc. All rights reserved.


*


*/


package cn.pingyutech.base.action;




import java.io.Serializable;


import java.util.Collection;




import org.apache.log4j.Logger;




import cn.pingyutech.base.service.IBaseService;




import com.opensymphony.xwork2.ActionSupport;






/** *//**


* Base action using generic


*/




public abstract class BaseAction<T, FK extends Serializable> extends ActionSupport...{




private static final Logger log = Logger.getLogger(BaseAction.class);




private static final long serialVersionUID = 1L;




private Collection<T> entities;




private T current;




private FK id;




public abstract IBaseService<T, FK> getService();






/** *//**


* 列出所有数据


*


* @return SUCCESS: OK; INPUT: Failure.


*/




public String list() ...{




try ...{


entities = this.getService().findByAll();


return execute();




} catch (Exception e) ...{


log.debug(e);


return INPUT;


}


}






/** *//**


* 根据id载入实体。


*


* @return


*/




public String load() ...{


this.current = (T) this.getService().load(id);


return SUCCESS;


}






/** *//**


* 保存当前实体,使用了SaveOrUpdate方法。


*


* @return


*/




public String save() ...{


this.getService().save(current);


return SUCCESS;


}






/** *//**


* 删除当前实体。


*


* @return


*/




public String delete() ...{


this.getService().delete(current);


return SUCCESS;


}






/** *//**


* @return the entities


*/




public Collection<T> getEntities() ...{


return entities;


}






/** *//**


* @param entities the entities to set


*/




public void setEntities(Collection<T> entities) ...{


this.entities = entities;


}






/** *//**


* @return the current


*/




public T getCurrent() ...{


return current;


}






/** *//**


* @param current the current to set


*/




public void setCurrent(T current) ...{


this.current = current;


}






/** *//**


* @return the id


*/




public FK getId() ...{


return id;


}






/** *//**


* @param id the id to set


*/




public void setId(FK id) ...{


this.id = id;


}


}

而继承BaseAction的UserAction如下:


package test;




import org.apache.log4j.Logger;




import cn.pingyutech.base.action.BaseAction;


import cn.pingyutech.base.service.IBaseService;






public class CustomerAction extends BaseAction<Customer, Integer> ...{




private static final Logger log = Logger.getLogger(CustomerAction.class);




private static final long serialVersionUID = 1L;




private CustomerDao custDao;




private int cid;






public CustomerAction() ...{


super();


}






/** *//**


* Used by DWR to delete a entity.


*


* @param id


*/




public String remove(int id)...{




try ...{


this.getService().delete(new Customer(id));


return SUCCESS;




} catch (Exception e) ...{


log.debug(e);


return INPUT;


}


}




// ======= getters/setters ========= //








public int getCid() ...{


return cid;


}






public void setCid(int cid) ...{


this.cid = cid;


this.setId(cid);


}






public CustomerDao getCustDao() ...{


return custDao;


}






public void setCustDao(CustomerDao dao) ...{


this.custDao = dao;


}






/** *//**


* <p>这个方法是继承父类BaseCRUDDao的,是为了自定义Dao而设立的。</p>


*/


@Override




public IBaseService<Customer, Integer> getService() ...{


return custDao;


}




}

暂且做个记录,待有时间了再好好研究
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: