使用& lt; ui: repeat> & lt; h: inputText>无法清除(重置)



场景如下:-

ControllerBean : -

private List<Attribute> attributeList;
//setter getters of the attributeList
属性>:-
private String attributeName;
private String attributeValue;
//setter getters of the attributeName and attributeValue
JSF代码

:

<ui:repeat var="attribute" 
    value="#{controllerBean.attributeList}">
   <h:outputLabel value="#{attribute.attributeName}"/>
   <h:inputText value="#{attribute.attributeValue}">
</ui:repeat>

: -

一切正常,所有的值都与列表绑定。当在文本框中填充attributeValue时,它在相应的属性中更新。当我更改属性的值时,它也会反映在后台bean中。

但是当我试图清除文本框的内容时,它不会将attributeValue设置为空字符串(或null)。它将先前填充的值保留在bean中。因此,用户永远无法将特定属性的值更新为null。

知道为什么吗?我该怎么解决这个问题呢?

您没有关闭<h:inputText>,并且使用@ViewScoped的给定代码,我能够保存空值。添加到commandButton action属性的方法打印出列表的每个元素,看看它是否真的没有改变。

支持Bean

@Named(value = "cbean")
@ViewScoped
public class Cbean implements Serializable{
    private List<Attribute> attributeList = new ArrayList<>();
    public List<Attribute> getAttributeList() {
        return attributeList;
    }
    public Cbean() {
    }
    @PostConstruct
    private void init() {
        for (int i = 0; i < 10; i++) {
            Attribute a = new Attribute();
            a.setAttributeName("name" + i);
            a.setAttributeValue("value" + i);
            attributeList.add(a);
        }
    }
    public void print() {
        for (Attribute a : attributeList) {
            System.out.println("name = " + a.getAttributeName() + " value = " + a.getAttributeValue());
        }
    }
}
XHTML

<h:form>
    <ui:repeat var="attribute" 
               value="#{cbean.attributeList}">
        <h:outputLabel value="#{attribute.attributeName}"/>
        <h:inputText value="#{attribute.attributeValue}"/>
    </ui:repeat>
    <h:commandButton value="Submit" action="#{cbean.print()}" />
</h:form>

删除第3元素内容后:

Info:   name = name0 value = value0
Info:   name = name1 value = value1111
Info:   name = name2 value =
Info:   name = name3 value = value3
Info:   name = name4 value = value4
Info:   name = name5 value = value5
Info:   name = name6 value = value6
Info:   name = name7 value = value7
Info:   name = name8 value = value8
Info:   name = name9 value = value9

相关内容

最新更新