同事代碼:
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 }
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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

檢查了半天,發現只要不執行initSelectValues,都沒有問題,后來一步一步調試才發現,原來是數據類型的問題,同事傳入的是List<Integer> values,而SelectItem構造函數支持的是鍵值都為String的參數,數據類型不對導致了這一個奇怪的現象,但后臺沒有異常和出錯信息讓人很納悶,解決方法就是只要改變值列表為List<String>就可以了。
---------------------------------------------------------
專注移動開發
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian