struts2对泛型的一个bug

wuliaolll 2011-03-28
我用struts2的action作通用查询处理器,用的是泛型

写了一个公共父类
// 用K作为条件查T
public abstract class QueryAction<T, K extends QueryCondition> extends
		ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9210459210082241477L;

	private PageBean<T> page;

	private int pageNum; // 第几页

	private int pageSize = DEFAULT_PAGE_SIZE; // 每页大小

	/**
	 * 表示是否为分页查询,默认为false即可
	 */
	private boolean pageQuery;

	private String conditionKey;

	public boolean isPageQuery() {
		return pageQuery;
	}

	public void setPageQuery(boolean pageQuery) {
		this.pageQuery = pageQuery;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private static final int DEFAULT_PAGE_SIZE = 10;

	public QueryAction() {
		super();
	}

	public QueryAction(String conditionKey) {
		this.conditionKey = conditionKey;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		if (getCondition() == null)
		{
			setCondition(getConditionInstance());
		}
		if (pageQuery) {
			this.setCondition((K) ServletActionContext.getRequest()
					.getSession().getAttribute(conditionKey));
		} else {
			ServletActionContext.getRequest().getSession().setAttribute(
					conditionKey, this.getCondition());
		}
		this.page = query();
		return SUCCESS;
	}

	protected abstract PageBean<T> query();

	public abstract K getCondition();
	
	public abstract K getConditionInstance();

	public abstract void setCondition(K condition);

	public PageBean<T> getPage() {
		return page;
	}

	public void setPage(PageBean<T> page) {
		this.page = page;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

}



子类去继承该父类,具体化泛型类。

public class SusBasicRepairQueryAction extends
		QueryAction<SusRepCusRelation, SusQueryCondition> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3816102436819904572L;

	public static final String SUS_BASIC_REPAIR_QUERY_KEY = "sus_basic_repair_query_condition";

	private SusQueryCondition condition;

	private SusProcessService susProcessService;

	/**
	 * @return the susProcessService
	 */
	public SusProcessService getSusProcessService() {
		return susProcessService;
	}

	/**
	 * @param susProcessService
	 *            the susProcessService to set
	 */
	public void setSusProcessService(SusProcessService susProcessService) {
		this.susProcessService = susProcessService;
	}

	public SusBasicRepairQueryAction() {
		super(SUS_BASIC_REPAIR_QUERY_KEY);
	}

	@Override
	public SusQueryCondition getCondition() {
		return condition;
	}

	@Override
	public SusQueryCondition getConditionInstance() {
		return new SusQueryCondition();
	}

	@Override
	protected PageBean<SusRepCusRelation> query() {
		return susProcessService.queryRepCusRelationList(getPageSize(),
				getPageNum(), condition);
	}

	@Override
	public void setCondition(SusQueryCondition condition) {
		this.condition = condition;
	}




结果是struts2容器偶尔在实例化子类的查询条件类时报ClassCastException,在这个方法报错
@Override
public void setCondition(SusQueryCondition condition) {
this.condition = condition;
}

应该是把参数condition实例化成了父类,但方法参数是子类,子类的引用指向了父类,从而报错,说明struts2实例化时,对泛型支持不太好,注意:是有频率地报错,不是每次报错。
Global site tag (gtag.js) - Google Analytics