最近写了一个JSF的动态控件生成框架给项目组使用。昨天有个同事在使用的过程中遇到一个问题,就是我生成的带AjaxSupport的动态控件在调用他自己写的方法之后出现了整个页面的action均无效的情况,现象就是,两个级联下拉,当地一个下拉选项改变时,触发他写的方法,他改变第二个下拉,但是当重复该操作之后就无效了,也没有错误和异常,代码如下: 
同事代码:
 1 public void bignessDangeValueChangeListener(ActionEvent e) {
 2         //System.out.println(e.getComponent().getParent().getChildCount());
 3         org.ajax4jsf.component.html.HtmlAjaxSupport ajaxSupport = (org.ajax4jsf.component.html.HtmlAjaxSupport)e.getComponent();
 4         HtmlSelectOneMenu parent = (HtmlSelectOneMenu)ajaxSupport.getParent();
 5         Short value = (Short)parent.getValue();
 6         String[] idDetails = parent.getId().split("_");
 7         StringBuffer controlId = new StringBuffer();
 8         controlId.append(idDetails[0]);
 9         if (WebUtil.PROPS_FILE_NAME_TRASH_DISCHARGE.equals(idDetails[0])) {
10             idDetails[1]="hazardousSubstance"; //配置文件中配置的二级联动的二级控件的name字段
11         }
12         
13         for (int i = 1;i < idDetails.length;i++){
14             controlId.append("_").append(idDetails[i]);
15         }
16         
17         ajaxSupport.setReRender(controlId.toString());
18         
19         List<BignessDangerInfo> list = Common.getBignessDangerInfoByObjType(value);
20         List<String> labelList = new ArrayList<String>();
21         List<Integer> valueList = new ArrayList<Integer>();
22         for(BignessDangerInfo bdi : list) {
23             labelList.add(bdi.getObjectName());
24             valueList.add(String.valueOf(bdi.getRecuid()));
25         }
26         
27 
28         
29         if(value == 1 || value == 2 || value == 3 || value == 4) {
30             DynamicControlFactory.initSelectValues(e, idDetails[1], labelList, valueList);
31         }  else {
32             List<List<String>> listClear = new ArrayList<List<String>>();
33             List<String> temp1 = new ArrayList<String>();
34             List<String> temp2 = new ArrayList<String>();
35             temp1.add("-1");
36             temp2.add("--请选择--");
37             listClear.add(temp1);
38             listClear.add(temp2);
39             DynamicControlFactory.initSelectValues(e, idDetails[1], listClear.get(1), listClear.get(0));
40         }
41     }
 
initSelectValues的相关代码:
 1
public static void initSelectValues(ActionEvent e, String col, List<String> labels, List<String> values)
 2
    
{
 3
        try 
{
 4
 5
            String[] parms = e.getComponent().getParent().getId().split("_");
 6
            DynamicControlObject dco = getControlObjects().get(parms[0]);
 7
            
 8
            List<String> colList = dco.getColNameList();
 9
            String prefix = dco.getFormName() + ":" + parms[0];
10
            
11
            Map cos = new HashMap();
12
            String rowIdx = parms[parms.length - 1];
13
14
            initSelectOneComponent(findComponent(prefix + "_" + col + "_1" + "_"
15
                    + rowIdx),  values, labels, true);
16
        } catch (Exception ex) 
{
17
            ex.printStackTrace();
18
        }
19
    } 
 1
public static void initSelectOneComponent(UIComponent component,
 2
            List<String> values, List<String> labels, boolean clearBefore) 
{
 3
        try 
{
 4
 5
            if (component == null || values == null || labels == null
 6
                    )
 7
                return;
 8
 9
            if (!isMultiValue(component))
10
                return;
11
12
            Object optionKey = null;
13
            String optionLabel = null;
14
            ArrayList optionsList = null;
15
16
            UISelectItems items = new UISelectItems();
17
            optionsList = new ArrayList(values.size());
18
            for (int i = 0; i < values.size(); i++) 
{
19
                optionKey = values.get(i);
20
                optionLabel = (String)labels.get(i);
21
                SelectItem item = new SelectItem(optionKey, optionLabel);
22
                optionsList.add(item);
23
            }
24
            items.setValue(optionsList);
25
            if(clearBefore)
26
            
{
27
                List list = component.getChildren();
28
                Object obj = null;
29
                for(int i = 0;i < list.size(); i ++)
30
                
{
31
                    if(list.get(i) instanceof UISelectItems )
32
                    
{
33
                        obj = list.get(i);
34
                        break;
35
                    }
36
                }
37
                if(obj != null)
38
                    list.remove(obj);
39
            }
40
                
41
            component.getChildren().add(items);
42
        } catch (Exception e) 
{
43
            e.printStackTrace();
44
        }
45
46
    } 
检查了半天,发现只要不执行initSelectValues,都没有问题,后来一步一步调试才发现,原来是数据类型的问题,同事传入的是List<Integer> values,而SelectItem构造函数支持的是键值都为String的参数,数据类型不对导致了这一个奇怪的现象,但后台没有异常和出错信息让人很纳闷,解决方法就是只要改变值列表为List<String>就可以了。
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
	
posted on 2009-11-18 09:02 
TiGERTiAN 阅读(1532) 
评论(2)  编辑  收藏  所属分类: 
Java 、
JSF