在jsf中动态添加radio和InputText [英] dynamic adding radio and InputText in jsf

查看:143
本文介绍了在jsf中动态添加radio和InputText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在动态生成的textBox中进行编辑时遇到问题.

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

查看我的代码.

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

这是我的豆子.

@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());
    }

以下是Pojo中具有getter和setter的三个变量

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.

谢谢.

推荐答案

您的具体问题是由于您使用的是JSTL <c:forEach>标记引起的,该标记在视图范围内运行,而在使用视图作用域的bean时.视图范围的Bean以视图状态存储.当您将表单提交到服务器时,视图状态将被恢复,但是那时原始视图作用域bean尚不可用,因此将创建一个新的bean(因此,所有属性均设置为default!).还原视图后,原始视图范围的Bean将放回范围内,覆盖临时视图.

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.

您需要一个完全有价值的JSF UI组件,而不是JSTL标记.为此,您需要<h:dataTable>.

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

替换

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

另请参见:

  • JSF2 Facelets中的JSTL ...有意义吗?
  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • 无关与具体问题无关,您在构造函数中存在一些代码重复.只需在其中调用addNew()方法.我还将删除实例变量i,因为这没有任何意义.只需使用本地的,然后使用List#size()进行初始化即可.

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

      这篇关于在jsf中动态添加radio和InputText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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