dynamic adding radio and InputText in jsf

无人久伴 提交于 2020-01-06 05:49:14

问题


i am getting problem in editing in textBox which are generated dynamically.

see my code.

<h:form>
                <h:panelGroup>
                    <h:panelGrid columns="2"> 
                        <h:panelGrid columns="1"> 
                            <h:selectOneRadio id="radio1" value="#{dynamicBean.radiovalue}" layout="pageDirection" >
                                <f:selectItems value="#{dynamicBean.objectList}" var="k1" itemValue="#{k1.value}" itemLabel="" />
                            </h:selectOneRadio>
                        </h:panelGrid>
                        <h:panelGrid columns="1" rowClasses="raw1">
                            <c:forEach items="#{dynamicBean.objectList}" var="k3">
                                <p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" >
                                </p:inputText>
                            </c:forEach>
                        </h:panelGrid>
                        <h:commandButton value="add new" action="#{dynamicBean.addNew}"/>
                    </h:panelGrid>
                </h:panelGroup>
            </h:form>

and this is my bean.

@ManagedBean
@ViewScoped
public class DynamicBean implements Serializable{

    private String radiovalue;
    private List<Pojo> objectList=new ArrayList<Pojo>();
    int i=0;
    private Pojo single=new Pojo();

    public DynamicBean() {   
        System.out.println("In Cons");
        if(objectList.isEmpty())
        {
            Pojo p1=new Pojo();
            p1.setName("Name-"+i);
            p1.setValue("Value-"+i);
            p1.setTextvalue("Text-"+i);
            objectList.add(p1);
            i++;
            setRadiovalue(p1.getValue());
        }
    }

    public void addNew()
    {   
        Pojo p1=new Pojo();
        p1.setName("Name-"+i);
        p1.setValue("Value-"+i);
        p1.setTextvalue("Text-"+i);
        objectList.add(p1);
        i++;
        setRadiovalue(p1.getValue());
    }

    public void ajaxEvent(ValueChangeEvent e)
    {
        System.out.println("New:"+e.getNewValue());
        System.out.print("Old:"+e.getOldValue());
    }

following are three variable in Pojo with getter and setter

private String name;
    private String value;
    private String textvalue;

biggest confusion is i can change first object value on in text box but i cant change the value of new generated objects.

Thanks.


回答1:


Your concrete problem is caused because you're using JSTL <c:forEach> tag which runs during view build time while you're using a view scoped bean. View scoped beans are stored in the view state. When you submit the form to the server, the view state will be restored, but the original view scoped bean isn't available yet at that moment and thus a new one will be created (thus, with all properties set to default!). After restoring the view, the original view scoped bean will be put back in scope, overriding the temporary one.

You need a fullworthy JSF UI component instead of a JSTL tag. For this particular purpose, you need the <h:dataTable>.

Replace

<h:panelGrid columns="1" rowClasses="raw1">
    <c:forEach items="#{dynamicBean.objectList}" var="k3">
        <p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" >
        </p:inputText>
    </c:forEach>
</h:panelGrid>

by

<h:dataTable value="#{dynamicBean.objectList}" var="k3" rowClasses="raw1">
    <h:column>
        <p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" />
    </h:column>
</h:dataTable>

See also:

  • JSTL in JSF2 Facelets... makes sense?

Unrelated to the concrete problem, you've some code duplication there in the constructor. Just call addNew() method in there. I'd also remove the instance variable i as that makes no sense. Just use a local one instead which get initialized with List#size().

public DynamicBean() {
    addNew();
}

public void addNew() {
    Pojo p1 = new Pojo();
    int i = objectList.size();
    p1.setName("Name-" + i);
    p1.setValue("Value-" + i);
    p1.setTextvalue("Text-" + i);
    objectList.add(p1);
    setRadiovalue(p1.getValue());
}


来源:https://stackoverflow.com/questions/10567446/dynamic-adding-radio-and-inputtext-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!