如何在JSF托管bean中检索通过循环生成的文本框的值



我需要通过如下循环生成文本框。

<p:panel id="dataPanel"  closable="true" toggleOrientation="horizontal" toggleable="true" header="Data">
    <h:panelGrid id="dataPanelGrid" columns="3" cellpadding="5">
        <c:forEach var="row" items="#{zoneChargeManagedBean.list}">
            <p:outputLabel for="txtCharge" value="#{row[1]}"/>          
            <p:inputText id="txtCharge" value="#{row[2]}" converter="#{bigDecimalConverter}" onkeydown="return isNumberKey(event, this.value);" label="#{row[1]}" required="false" maxlength="45">
                <f:validator validatorId="negativeNumberValidator"/>
                <f:attribute name="isZeroAllowed" value="false"/>
                <f:validator validatorId="bigDecimalRangeValidator"/>
                <f:attribute name="minPrecision" value="1"/>
                <f:attribute name="maxPrecision" value="33"/>
                <f:attribute name="scale" value="2"/>
            </p:inputText>
            <p:message for="txtCharge" showSummary="false"/>
        </c:forEach>
        <p:commandButton id="btnSubmit" update="dataPanel messages" actionListener="#{zoneChargeManagedBean.insert}" icon="ui-icon-check" value="Save"/>
        <p:commandButton value="Reset" update="dataPanel" process="@this">
            <p:resetInput target="dataPanel" />
        </p:commandButton>
    </h:panelGrid>
</p:panel>

给定文本框的值是数据库中的BigDecimal类型。

当按下给定的命令按钮时,应该从相应的JSF托管bean中检索这些文本框所包含的值,以便在数据库中插入或更新它们。

如果按下给定按钮时,可以在某种集合(如java.util.List)中同时检索所有这些文本字段的值,那就更好了。

<ui:repeate>,一个渲染时间标记工作正常,但<c:foreEach>,一个视图构建时间组件工作不正常(我无法说明原因),但在这种特殊情况下,我发现<p:dataGrid>更合适。XHTML进行了相应的修改,如下所示。

<p:panel id="dataPanel" rendered="#{zoneChargeManagedBean.renderedDataPanel}" closable="true" toggleOrientation="horizontal" toggleable="true" header="Data">
    <p:dataGrid columns="3" value="#{zoneChargeManagedBean.list}" var="row" paginator="true" paginatorAlwaysVisible="false" pageLinks="10" rows="15">
        <p:watermark for="txtCharge" value="Enter charge."/>
        <p:tooltip for="lblCharge" value="Some message."/>
        <p:column>
            <p:outputLabel id="lblCharge" for="txtCharge" value="#{row[1]}"/><br/>
            <p:inputText id="txtCharge" value="#{row[2]}" onkeydown="return isNumberKey(event, this.value);" converter="#{bigDecimalConverter}" label="#{row[1]}" required="false" maxlength="45">
                <f:validator validatorId="negativeNumberValidator"/>
                <f:attribute name="isZeroAllowed" value="false"/>
                <f:validator validatorId="bigDecimalRangeValidator"/>
                <f:attribute name="minPrecision" value="1"/>
                <f:attribute name="maxPrecision" value="33"/>
                <f:attribute name="scale" value="2"/>
            </p:inputText>
            <h:message for="txtCharge" showSummary="false" style="color: #F00;"/>
        </p:column>
    </p:dataGrid>
    <p:commandButton id="btnSubmit" update="dataPanel messages" actionListener="#{zoneChargeManagedBean.insert}" icon="ui-icon-check" value="Save"/>
    <p:commandButton value="Reset" update="dataPanel" process="@this">
        <p:resetInput target="dataPanel" />
    </p:commandButton>
</p:panel>

托管bean:

@Controller
@Scope("view")
public final class ZoneChargeManagedBean implements Serializable
{
    @Autowired
    private final transient ZoneChargeService zoneChargeService=null;
    private ZoneTable selectedZone;     //Getter and setter
    private List<Object[]>list;         //Getter and setter
    private boolean renderedDataPanel;  //Getter and setter
    public ZoneChargeManagedBean() {}
    public void ajaxListener() {
        if(this.selectedZone!=null){
            list=zoneChargeService.getZoneChargeList(this.selectedZone.getZoneId());
            renderedDataPanel=true;
        }
        else {
            renderedDataPanel=false;
        }
    }
    public void insert() {
        //Just do whatever is needed based on the list with new values which is retrieved when <p:commandButton> as shown in the given XHTML is clicked.
        if(selectedZone!=null&&zoneChargeService.addOrUpdate(list, selectedZone)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Summary", "Message"));
        }
        else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Message Summary", "Message"));
        }
    }
}

ajaxListener()方法中的服务方法返回对象数组的类型列表List<Object[]>

public List<Object[]>getZoneChargeList(Long id) {
    return entityManager.createQuery("select w.weightId, w.weight, zc.charge from Weight w left join w.zoneChargeSet zc with zc.zoneTable.zoneId=:id order by w.weight").setParameter("id", id).getResultList();
}

我不能使用相应的JPA标准查询,因为with运算符似乎不受JPA标准API的支持

当选择了<p:selectOneMenu>中未包含的项目时,会调用此方法。

最新更新