这个问题实际上是这里所说的问题的下一步:
如何将动态创建的 HtmlInputText 组件的值绑定到 bean 属性?
所以下一步是实际保存用户使用上一个问题中生成的动态表单(以编程方式创建的 jsf 表单,由简单的 inputexts 组成)提交的值。以下是每个组件的代码:
该模型:
@Entity
@Table(name = "imageviewer_crreviewerformdata")
public class CRReviewerFormData implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "FdId")
private Long fdId;
@Column(name = "Input1")
private String input1;
@Column(name = "Input2")
private String input2;
@Column(name = "Input3")
private String input3;
/* getters & setters*/
// ...
风景:
<p:commandButton value="View" action="#{reviewReportBean.updateDynamicPanelGrid()}"
oncomplete="dlg.show()" icon="ui-icon-image" >
<f:param name="selectedImage" value="#{cRImageData.imName}" />
</p:commandButton>
...
<p:outputPanel id="outerContainerDynamicPanelGrid" autoUpdate="true">
<h:panelGrid id="innerContainerDynamicPanelGrid"
binding="#{reviewReportBean.dynamicPanelGrid}">
</h:panelGrid>
</p:outputPanel>
<p:commandButton id="viewSaveForm" value="Save"
action='#{reviewReportBean.saveReport()}'>
</p:commandButton>
<p:commandButton id="viewEditForm" value="Edit"
action='#{reviewReportBean.editReport()}'>
</p:commandButton>
...
控制器:
@ManagedBean(name = "reviewReportBean")
@ViewScoped
public class ReviewReportBean implements Serializable {
private static final long serialVersionUID = 1L;
private String imageOfInterest;
private HtmlPanelGrid dynamicPanelGrid;
private CRReviewerFormData cRReviewerFromData;
// ...
@PostConstruct
public void init(){
dynamicPanelGrid = new HtmlPanelGrid();
FacesContext facesContext = FacesContext.getCurrentInstance();
ConfigOptionsBean configOptionsBean = (ConfigOptionsBean) facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "configOptionsBean");
cRReviewerFromData = new CRReviewerFormData();
}
@SuppressWarnings("unused")
public void updateDynamicPanelGrid() {
RequestContext requestContext = RequestContext.getCurrentInstance();
Application application = FacesContext.getCurrentInstance().getApplication();
dynamicPanelGrid.getChildren().clear();
Row row1 = (Row) application.createComponent(Row.COMPONENT_TYPE);
row1.setRendered(true);
Row row2 = (Row) application.createComponent(Row.COMPONENT_TYPE);
row2.setRendered(true);
Row row3 = (Row) application.createComponent(Row.COMPONENT_TYPE);
row3.setRendered(true);
HtmlOutputLabel label1 = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
label1.setValue("I am the first label");
label1.setStyle("font-weight:bold;color:black");
label1.setId("label1");
HtmlOutputLabel label2 = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
label2.setValue("I am the second label");
label2.setStyle("font-weight:bold;color:red");
label2.setId("label2");
HtmlOutputLabel label3 = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
label3.setValue("I am the third label");
label3.setStyle("font-weight:bold;color:red");
label3.setId("label3");
HtmlInputText input1 = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
input1.setId("input1");
input1.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory()
.createValueExpression(FacesContext.getCurrentInstance()
.getELContext(), "#{reviewReportBean.input1}" , String.class));
HtmlInputText input2 = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
input2.setId("input2");
input2.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory()
.createValueExpression(FacesContext.getCurrentInstance()
.getELContext(), "#{reviewReportBean.input2}" , String.class));
HtmlInputText input3 = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
input3.setId("input3");
input3.setValueBinding(arg0, arg1);
input3.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory()
.createValueExpression(FacesContext.getCurrentInstance()
.getELContext(), "#{reviewReportBean.input3}" , String.class));
dynamicPanelGrid.setColumns(2);
dynamicPanelGrid.getChildren().add(label1);
dynamicPanelGrid.getChildren().add(input1);
dynamicPanelGrid.getChildren().add(label2);
dynamicPanelGrid.getChildren().add(input2);
dynamicPanelGrid.getChildren().add(label3);
dynamicPanelGrid.getChildren().add(input3);
requestContext.update(":viewDatagridForm:innerContainerDynamicPanelGrid");
}
// ...
}
如何将动态创建的HtmlInputText
组件的值保存到 bean 属性?
我在控制器(reviewReportBean)中实现了一个典型的 Save() 方法,如下所示:
public String saveReport() {
String result = null;
System.out.println(">>>> method to save form called!");
Session session = HibernateUtil.getSessionFactory().openSession();
CRReviewerFormData cRReviewerFormData = new CRReviewerFormData();
cRReviewerFormData.setInput1(this.getInput1());
cRReviewerFormData.setInput2(this.getInput2());
cRReviewerFormData.setInput3(this.getInput3());
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.save(cRReviewerFormData);
tx.commit();
result = SUCCESS;
}
catch (Exception e)
{
if (tx != null)
{
tx.rollback();
result = ERROR;
e.printStackTrace();
}
}
finally
{
session.close();
}
return result;
}
当我尝试使用保存按钮时,我收到以下错误:
16:44:08,632 ERROR [ExceptionHandlerAjaxImpl:57] Component ID A3702:imageEditor:label1 has already been found in the view.
java.lang.IllegalStateException: Component ID A3702:imageEditor:label1 has already been found in the view.
at com.sun.faces.util.Util.checkIdUniqueness(Util.java:821)
at com.sun.faces.util.Util.checkIdUniqueness(Util.java:805)
at com.sun.faces.util.Util.checkIdUniqueness(Util.java:805)
at com.sun.faces.util.Util.checkIdUniqueness(Util.java:805)
at com.sun.faces.util.Util.checkIdUniqueness(Util.java:805)
at com.sun.faces.util.Util.checkIdUniqueness(Util.java:805)
at com.sun.faces.application.view.StateManagementStrategyImpl.saveView(StateManagementStrategyImpl.java:144)
再往下一点我得到:
SEVERE: Servlet.service() for servlet ImageViewer Servlet threw exception
javax.portlet.faces.BridgeException: java.lang.IllegalStateException: CDATA tags may not nest
表单ID有什么实际问题吗?
我已经实现了足够的 Save 方法实现还是我必须设置其他任何东西?