我试图使用jsf和一个生成表单元素的后bean来动态地呈现一个表单。
这是后端bean的代码:
@ManagedBean(name = "reviewReportBean")
@SessionScoped
public class ReviewReportBean implements Serializable {
... ... ...
private HtmlPanelGrid dynamicPanelGrid;
... ... ...
public HtmlPanelGrid dynamicPanelGrid() {
Application application = FacesContext.getCurrentInstance().getApplication();
List childrenList = dynamicPanelGrid.getChildren();
childrenList.clear();
... ... ...
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try
{
... ... ...
HtmlOutputLabel label = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
label.setValue("Mpla Mpla Mpla");
... ... ...
childrenList.add(label);
... ... ...
String hq9 = "select ... WHERE it (:itid)";
SQLQuery query9 = session.createSQLQuery(hq9);
query9.setParameter("itid",imageTypeId);
query9.addEntity(ReportFormFields.class);
reportFormFields = query9.list();
for(int i=0; i<reportFormFields.size(); i++){
System.out.println("-------------------------");
System.out.println(">> Field Id: " + reportFormFields.get(i).getRffFieldId());
System.out.println(">> Field Name: " + reportFormFields.get(i).getRffFieldName());
... ... ...
HtmlOutputLabel output = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
output.setValue(reportFormFields.get(i).getRffFieldLabel());
childrenList.add(output);
if(reportFormFields.get(i).getRffFieldType().equals("Option List")){
HtmlSelectOneMenu combomenu = (HtmlSelectOneMenu)application.createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
UISelectItems comboItems = new UISelectItems();
String listOptions =reportFormFields.get(i).getRffFieldOptions();
String array[] = listOptions.split("(?<!\\\\),");
for(int j = 0;j<array.length;j++){
System.out.println(">>> list option " + j + " is " + array[j]);
}
comboItems.setValue(array);
combomenu.getChildren().add(comboItems);
childrenList.add(combomenu);
}else{
HtmlInputText input = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
childrenList.add(input);
}
... ... ...
}
... ... ...
}
... ... ...
return dynamicPanelGrid;
}
}
这就是我尝试使用back bean代码在我的视图(xhtml)中动态生成表单的方式:
<h:panelGrid id="dynaPanelGrid7"
binding="#{reviewReportBean.dynamicPanelGrid}" >
</h:panelGrid>
但根本不会生成表单。但是,如果我称之为(直接调用方法以生成表单):
<h:panelGrid id="dynaPanelGrid7"
binding="#{reviewReportBean.dynamicPanelGrid()}" >
</h:panelGrid>
我收到以下错误:
can't parse argument number reviewReportBean.dynamicPanelGrid(): /views/view.xhtml @199,63 binding="#{reviewReportBean.dynamicPanelGrid()}": Illegal Syntax for Set Operation
但如果随后我尝试将其称为:
<h:panelGrid id="dynaPanelGrid7"
binding="#{reviewReportBean.dynamicPanelGrid}" >
</h:panelGrid>
表单呈现!!
所以我想填充对象dynamicPanelGrid ......
有关如何更改代码以使表单正常工作的任何想法?
答案 0 :(得分:0)
dynamicPanelGrid
是您的字段(属性)的名称。 Getter和setter应如下所示:
@PostConstruct
public void init() {
// init your dynamicPanelGrid
}
public HtmlPanelGrid getDynamicPanelGrid() {
return dynamicPanelGrid;
}
public void setDynamicPanelGrid(HtmlPanelGrid dynamicPanelGrid) {
this.dynamicPanelGrid = dynamicPanelGrid;
}
了解Java Beans。
比你的绑定属性应该如下所示:
binding="#{reviewReportBean.dynamicPanelGrid}"