关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录
1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误
解决办法很简单,不能让list为空
2
刚进入该界面的时候list是有数据的,当点击提交等按钮的时候,数据被提交到后台,如果配置了验证框架或者在action中写了validate方法,校验没有通过,未走action,直接返回了input,又指定回了当前界面。
此时的checkboxlist中的list丢失了,导致了如上错误(这个错误提示的不太友好,让人认为是类转换错误)
解决办法是把初始化list的工作放到prepare拦截器中,因为prepare是在validate拦截器之前执行,即实现prepareble接口:
publicclass RoleAction extends ActionSupport implements Preparable{
@Override
publicvoid prepare() throws Exception {
//初始化list
}
}
意思就是你这个类实现接口Preparable里的prepare,把原先获取list放入到prepare()方法中,然后在原先List语句上调用这个方法就OK
package com.ccse.frame.action.rule; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.annotation.Resource; import com.ccse.core.SeqManager; import com.ccse.core.command.QueryFilter; import com.ccse.core.command.sql.SqlQueryFilter; import com.ccse.core.command.sql.SqlSpellerDbType; import com.ccse.core.util.BeanUtil; import com.ccse.core.web.action.BaseAction; import com.ccse.frame.model.base.BaseUser; import com.ccse.frame.model.rule.RuleAutogeneration; import com.ccse.frame.service.rule.RuleAutogenerationService; import com.ccse.oa.util.ContextUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.opensymphony.xwork2.Preparable; /** * 证书自动生成规则 * * @author hackermi * @date 2013-7-3 下午02:33:30 */ public class RuleAutogenerationAction extends BaseAction implements Preparable{ /** * */ private static final long serialVersionUID = 1L; @Resource private RuleAutogenerationService ruleAutogenerationService; private RuleAutogeneration ruleAutogeneration; private String ruleId; //创建新版本 private boolean ruleRoot = false; public boolean getRuleRoot() { return ruleRoot; } public void setRuleRoot(boolean ruleRoot) { this.ruleRoot = ruleRoot; } public String getRuleId() { return ruleId; } public void setRuleId(String ruleId) { this.ruleId = ruleId; } public RuleAutogeneration getRuleAutogeneration() { return ruleAutogeneration; } public void setRuleAutogeneration(RuleAutogeneration ruleAutogeneration) { this.ruleAutogeneration = ruleAutogeneration; } /** * 显示列表 */ public String list() { QueryFilter filter = new QueryFilter(getReq()); List<RuleAutogeneration> list = ruleAutogenerationService .getAll(filter); Type type = new TypeToken<List<RuleAutogeneration>>() { }.getType(); StringBuffer buff = new StringBuffer("{\"total\":").append( filter.getPagingBean().getTotalItems()).append(",\"rows\":"); Gson gson = new Gson(); buff.append(gson.toJson(list, type)); buff.append("}"); jsonString = buff.toString(); return SUCCESS; } /** * 批量删除 * * @return */ public String multiDel() { String ids = getReq().getParameter("ids"); if (ids != null) { for (String id : ids.split(",")) { ruleAutogenerationService.remove(id); } } jsonString = "{success:true}"; return SUCCESS; } public void prepare() throws Exception { // TODO Auto-generated method stub List sysTypes=ruleAutogenerationService.getsysType();//所属类型 List sysType=new ArrayList(); for (int i = 0; i < sysTypes.size(); i++) { System.out.println(sysTypes.get(i)); } Iterator<?> itr = sysTypes.iterator(); while (itr.hasNext()) { Map<?, ?> nextObj = (Map<?, ?>) itr.next(); String value=(String) nextObj.get("ywcode"); sysType.add(value.trim()); // sysType.add((String)nextObj.get("ywcode")); } this.getRequest().put("sysType", sysType); } /** * 新增SAMIS * * @return */ public String getSAMIS() { try { // List sourCeNames=ruleAutogenerationService.getSourCeName(); // this.getRequest().put("sourcenames", sourCeNames);//系统类型 String ruleRoot = this.getReq().getParameter("ruleRoot");// 是否是创建新版本 if (ruleId != null && !"".equals(ruleId)) { ruleAutogeneration = ruleAutogenerationService.get(ruleId); if (ruleRoot != null && !"".equals(ruleRoot)) { ruleAutogeneration.setRuleRoot(ruleRoot); this.getReq().setAttribute("ruleRoot", ruleRoot); } } this.prepare(); this.setForwardPage("/pages/rule/RuleAutogenerationSAMISForm.jsp"); jsonString = null; return SUCCESS; } catch (Exception e) { e.printStackTrace(); return this.err(e); } } /** * 新增SSMIS * * @return */ public String getSSMIS() { try { //List sourCeNames=ruleAutogenerationService.getSourCeName(); // this.getRequest().put("sourcenames", sourCeNames);//所属系统 String ruleRoot = this.getReq().getParameter("ruleRoot");// 是否是创建新版本 if (ruleId != null && !"".equals(ruleId)) { ruleAutogeneration = ruleAutogenerationService.get(ruleId); if (ruleRoot != null && !"".equals(ruleRoot)) { ruleAutogeneration.setRuleRoot(ruleRoot); this.getReq().setAttribute("ruleRoot", ruleRoot); } } this.setForwardPage("/pages/rule/RuleAutogenerationSSMISForm.jsp"); jsonString = null; return SUCCESS; } catch (Exception e) { e.printStackTrace(); return this.err(e); } } /** * 添加及保存操作 */ public String save() { try { BaseUser baseUser = ContextUtil.getCurrentUser(); //新增 if (ruleAutogeneration.getRuleId() == null || "".equals(ruleAutogeneration.getRuleId())) { ruleAutogeneration.setRuleId(null); ruleAutogeneration.setRuleVersion("1.0"); ruleAutogeneration.setRuleRoot(SeqManager.getSeqMang().getUUID());//根节点 ruleAutogeneration.setLastUpdateTime(new Date());// 修改时间 ruleAutogeneration.setCreTime(new Date());// 创建时间 ruleAutogeneration.setCreUserId(baseUser.getId());// 创建人ID ruleAutogeneration.setCreUserName(baseUser.getUsername());// 创建人姓名 ruleAutogenerationService.save(ruleAutogeneration); } else { RuleAutogeneration orgRuleAutogeneration = ruleAutogenerationService.get(ruleAutogeneration.getRuleId()); if(null != orgRuleAutogeneration) { BeanUtil.copyNotNullProperties(orgRuleAutogeneration, ruleAutogeneration); //创建新版本 if (ruleRoot) { ruleAutogenerationService.evict(orgRuleAutogeneration);//清空旧对象的缓存 orgRuleAutogeneration.setRuleId(null); orgRuleAutogeneration.setRuleVersion(ruleAutogenerationService.getBigVersion(orgRuleAutogeneration)); orgRuleAutogeneration.setLastUpdateTime(new Date());//修改时间 orgRuleAutogeneration.setCreTime(new Date());//创建时间 orgRuleAutogeneration.setCreUserName(baseUser.getUsername()); ruleAutogenerationService.save(orgRuleAutogeneration); } else { //修改 orgRuleAutogeneration.setLastUpdateTime(new Date());// 修改时间 orgRuleAutogeneration.setUpdateUserId(baseUser.getId());//修改创建人ID orgRuleAutogeneration.setUpdateUserName(baseUser.getUsername());// 修改人姓名 ruleAutogenerationService.save(orgRuleAutogeneration); } }else{ throw new RuntimeException("对象不存在"); } } } catch (Exception ex) { logger.error(ex.getMessage()); ex.printStackTrace(); } setJsonString("{success:true}"); if (ruleAutogeneration.getSysId().equals("SAMIS")) { this.setForwardPage("/pages/rule/RuleAutogenerationSAMISForm.jsp"); } else if (ruleAutogeneration.getSysId().equals("SSMIS")) { this.setForwardPage("/pages/rule/RuleAutogenerationSSMISForm.jsp"); } return SUCCESS; } }