我想将所有网格面板存储到一个arraylist中,以便我可以迭代它们。但是,得到的结果并不符合我的预期。使用Netbeans调试模式,我意识到无论添加到arraylist中的gridpanel,它都有与jsp绑定的值不同的值。
例如,(从调试模式)
Name Type Value
gridPanel1 HtmlPanelGrid #334
.
.
gridPanels ArrayList_ #323
[0] HtmlPanelGrid #524
问题是什么问题?这样做的正确方法是什么?
JSP文件
<h:panelGrid binding="#{CreatePage.gridPanel1}" columns="10" id="gridPanel1" style="height: 100%" width="671">
<webuijsf:label for="column_TF1" id="columnLabel1" text="Column 1: "/>
<webuijsf:textField binding="#{CreatePage.column_TF1}" id="column_TF1" valueChangeListenerExpression="#{CreatePage.column_TF1_processValueChange}"/>
<webuijsf:label for="dataDropDown1" id="dataLabel1" text="Data Type: "/>
<webuijsf:dropDown binding="#{CreatePage.dataDropDown1}" id="dataDropDown1"
items="#{CreatePage.dataDropDown1DefaultOptions.options}"
onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'mainGridPanel:columnGridPanel:gridPanel1:dataDropDown1');"
selected="#{CreatePage.dataDropDown1DefaultOptions.selectedValue}" valueChangeListenerExpression="#{CreatePage.dataDropDown1_processValueChange}"/>
<webuijsf:textField binding="#{CreatePage.charValueTF1}" columns="5" id="charValueTF1" rendered="false"/>
<webuijsf:label for="pkDropDown1" id="pkLabel1" text="Primary Key: "/>
<webuijsf:dropDown binding="#{CreatePage.pkDropDown1}" id="pkDropDown1"
items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange="" selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}"/>
<webuijsf:label for="nullableDropDown1" id="nullableLabel1" text="Nullable:"/>
<webuijsf:dropDown binding="#{CreatePage.nullableDropDown1}" id="nullableDropDown1"
items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange=""
selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}" valueChangeListenerExpression="#{CreatePage.nullableDropDown1_processValueChange}"/>
</h:panelGrid>
<h:panelGrid binding="#{CreatePage.gridPanel2}" columns="10" id="gridPanel2" style="height: 100%" width="671">
<webuijsf:label for="column_TF2" id="columnLabel2" text="Column 2: "/>
<webuijsf:textField binding="#{CreatePage.column_TF2}" id="column_TF2" valueChangeListenerExpression="#{CreatePage.column_TF2_processValueChange}"/>
<webuijsf:label for="dataDropDown2" id="dataLabel2" text="Data Type: "/>
<webuijsf:dropDown binding="#{CreatePage.dataDropDown2}" id="dataDropDown2"
items="#{CreatePage.dataDropDown1DefaultOptions.options}"
onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'mainGridPanel:columnGridPanel:gridPanel2:dataDropDown2');" selected="#{CreatePage.dataDropDown1DefaultOptions.selectedValue}"/>
<webuijsf:textField binding="#{CreatePage.charValueTF2}" columns="5" id="charValueTF2" rendered="false"/>
<webuijsf:label for="pkDropDown2" id="pkLabel2" text="Primary Key: "/>
<webuijsf:dropDown binding="#{CreatePage.pkDropDown2}" id="pkDropDown2"
items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange="" selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}"/>
<webuijsf:label for="nullableDropDown2" id="nullableLabel2" text="Nullable:"/>
<webuijsf:dropDown binding="#{CreatePage.nullableDropDown2}" id="nullableDropDown2"
items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange=""
selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}" valueChangeListenerExpression="#{CreatePage.nullableDropDown1_processValueChange}"/>
</h:panelGrid>
Java文件
List<HtmlPanelGrid> gridPanels = new ArrayList<HtmlPanelGrid>();
public HtmlPanelGrid getGridPanel1() {
return gridPanel1;
}
public void setGridPanel1(HtmlPanelGrid hpg) {
this.gridPanel1 = hpg;
}
private HtmlPanelGrid gridPanel2 = new HtmlPanelGrid();
public HtmlPanelGrid getGridPanel2() {
return gridPanel2;
}
public void setGridPanel2(HtmlPanelGrid hpg) {
this.gridPanel2 = hpg;
}
private HtmlPanelGrid gridPanel3 = new HtmlPanelGrid();
if (gridPanels.isEmpty()) {
gridPanels.add(gridPanel1);
gridPanels.add(gridPanel2);
gridPanels.add(gridPanel3);
gridPanels.add(gridPanel4);
gridPanels.add(gridPanel5);
gridPanels.add(gridPanel6);
}
for (int i = 0; i < gridPanels.size(); i++) {
TextField tempTF = (TextField) gridPanels.get(i).findComponent(("column_TF" + Integer.toString(i)));
// I always get null value
tempStr = (String) tempTF.getValue();
}
答案 0 :(得分:1)
我持续获得null值的原因是因为我没有从正确的位置获取值。
正确的代码应该是
for (int i = 0; i < gridPanels.size(); i++) {
// first get() returns the gridPanel
// second get() returns the components within the gridPanel.
// In this case, 1 is the TextField
TextField tempTF = (TextField) gridPanels.get(i).getChildren().get(1);
tempStr = (String) tempTF.getValue();
}