如何在JSF托管Bean中检索通过循环生成的文本框的值? [英] How to retrieve values of textboxes in JSF managed bean which are generated through a loop?

查看:37
本文介绍了如何在JSF托管Bean中检索通过循环生成的文本框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过循环生成文本框,如下所示.

I need to generate textboxes through a loop as follows.

<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类型.

The value of the given textbox is a type of BigDecimal from the database.

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

When the given command button is pressed, the values held by these textboxes should be retrieved from the corresponding JSF managed bean so that they can either be inserted or updated in the database.

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

It would be even better, if it is possible to retrieve the values of all of these text fields at once in some kind of collection (like java.util.List), when the given button is pressed.

推荐答案

<ui:repeate>,渲染时间标签正常工作,但<c:foreEach>,视图构建时间组件(我无法弄清原因),但在此特定情况下情况下,我发现<p:dataGrid>更合适. XHTML进行了相应的修改,如下所示.

<ui:repeate>, a render time tag works correctly but not <c:foreEach>, a view build time component (I can't clarify why) but in this particular case, I found <p:dataGrid> is more suitable. The XHTML has been modified accordingly as follows.

<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:


The managed 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()方法中的service方法返回对象数组类型的列表-List<Object[]>.

The service method as in the ajaxListener() method returns a list of type of an array of objects - 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标准查询,这是因为JPA标准API似乎不支持with运算符.

I can't use the corresponding JPA criteria query which is intended because the with operator which doesn't seem to be supported by the JPA criteria API.

被调用此方法时,从一个项目被选择,其不包括在这个问题.

This method is invoked when an item from <p:selectOneMenu> is selected which is not covered in this question.

这篇关于如何在JSF托管Bean中检索通过循环生成的文本框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆