Getting value from dynamically created inputText

2020-08-01 06:37发布

The following is my code snippet in my abc.xhtml page :

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

This is my panelGrid I am generating inputText dynamically depending numOfLable. After generation say 2 will be generate user will add some text to each inputText so my Question is How can I get value of dyanamically generated inputbox.

Thanks.

3条回答
一夜七次
2楼-- · 2020-08-01 06:47

You can bind the value into the bean value object:

<input type="text" id="inputTxt_${i}" value="${specificationMB.getValue(i).value}"  />
查看更多
爷的心禁止访问
3楼-- · 2020-08-01 06:53

This can easily be done with basics of JSF and primefaces. Here is full working Example:

XHTML File (I am using p:panel and 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>

Managed 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;
    }

}

Label Class

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;
    }

}

Output

Before entering data

After data entered and submitted it displays the values

查看更多
闹够了就滚
4楼-- · 2020-08-01 06:54

In order to get the values of your dynamically generated inputTexts. You may do something like this.

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

Then retrieve the text value by using this code in servlet

String inputText1 = request.getParameter("nameOfFirstInputText");
查看更多
登录 后发表回答