我正在尝试使用jsf和生成表单元素的backbean来动态地呈现表单。
这是后台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已经填充。。。
关于如何更改我的代码以使表单正常工作,有什么想法吗?
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}"