从动态创建的inputText中获取价值

时间:2013-12-31 11:05:09

标签: java jsf primefaces

以下是我的abc.xhtml页面中的代码片段:

<p:panelGrid id="pnlGrd_numOfLbl"
  style="align:center; width:100%;" cellpadding="5">
<c:forEach var="i" begin="1" end="${specificationMB.numOfLbl}" >
<p:row>
    <p:column width="50%">
        <p:outputLabel value="Label ${i}" />
    </p:column>
    <p:column width="50%">
        <p:inputText id="inputTxt_${i}" style="width:150px;" />
    </p:column>
</p:row>
</c:forEach>
</panelGrid>

这是我的panelGrid我依赖动态生成inputText     numOfLable。生成后说2将生成用户将添加一些     每个inputText的文本,所以我的问题是如何获得dyanamically的价值     生成输入框。

感谢。

3 个答案:

答案 0 :(得分:3)

这可以通过JSF和primefaces的基础知识轻松完成。这是完整的工作示例:

XHTML文件(我使用的是p:panel和ui:repeater)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">

    <h:head>
        <link rel=" stylesheet" type="text/css" href="css/style.css"></link>

    </h:head>

    <h:body>
        <h:form>
            <p:panel header="Panel">

                <ui:repeat var="lbl" value="#{tBean.lblClassess}">
                    <p:row>
                        <p:column width="50%">
                            <p:outputLabel value="#{lbl.lbl} :" />
                        </p:column>
                        <p:column width="50%">
                            <p:inputText value="#{lbl.value}" />
                        </p:column>
                    </p:row>
                </ui:repeat>

            </p:panel>

            <p:commandButton actionListener="#{tBean.submit}" value="Subtmi" update="values"></p:commandButton>

            <p:outputPanel id="values">

                <ui:repeat var="lbl" value="#{tBean.lblClassess}">
                    <p:row>
                        <p:column width="50%">
                            <p:outputLabel value="#{lbl.value} :" />
                        </p:column>

                    </p:row>
                </ui:repeat>

            </p:outputPanel>

        </h:form>
    </h:body>
</f:view>
<body>

</body>

</html>

托管Bean

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "tBean")
@ViewScoped
public class TestBean {

    private List<LabelClass> lblClassess;

    public TestBean() {
        lblClassess = new ArrayList<LabelClass>();
        lblClassess.add(new LabelClass("First Label", ""));
        lblClassess.add(new LabelClass("Second Label", ""));
        lblClassess.add(new LabelClass("Third Label", ""));

    }

    public void submit(ActionEvent e) {
        for (LabelClass lbl : lblClassess) {
            System.out.println(lbl.getValue());
        }
    }

    public List<LabelClass> getLblClassess() {
        return lblClassess;
    }

    public void setLblClassess(List<LabelClass> lblClassess) {
        this.lblClassess = lblClassess;
    }

}

标签类

public class LabelClass {
    private String lbl;
    private String value;

    public LabelClass(String lbl, String value) {
        super();
        this.lbl = lbl;
        this.value = value;
    }

    public String getLbl() {
        return lbl;
    }

    public void setLbl(String lbl) {
        this.lbl = lbl;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

<强> 输出

Before entering data

After data entered and submitted it displays the values

答案 1 :(得分:1)

为了获得动态生成的inputTexts的值。你可能会这样做。

<input type="text" id="inputTxt_${i}" name="inputTxt_${i}" style="width:150px;" />

然后在servlet中使用此代码检索文本值

String inputText1 = request.getParameter("nameOfFirstInputText");

答案 2 :(得分:0)

您可以将值绑定到bean值对象:

<input type="text" id="inputTxt_${i}" value="${specificationMB.getValue(i).value}"  />