0

Here is the scenario:-

ControllerBean:-

private List<Attribute> attributeList;
//setter getters of the attributeList

Attribute:-

private String attributeName;
private String attributeValue;
//setter getters of the attributeName and attributeValue

JSF code:

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

The issue:-

Everything works fine, all the values are binded with the list. When the attributeValue is filled in the text-box its updated in the corresponding attribute. When I change the value of the attribute, then too its reflected in the backing bean.

But when i try to clear the contents of the text-box then it does not set the attributeValue to empty string (or null). It keeps the previous filled value in the bean. As a result the user can never update the value of the specific attribute to null.

Any idea why? And what should I do to resolve this?

basim
  • 13
  • 5
  • Could you please post the complete contents of the three files? If the contents are long you could remove the irrelevent parts of code. – Javier Haro Sep 03 '15 at 22:20
  • @Iametaweb There is only irrelevent piece of code other than what I have posted. Is there any specific part of code that you want to see? Because if you just create a simple bean/controller/xhtml with the above information the issue can be still reproduced. Please reply if i still need to add anything? – basim Sep 04 '15 at 04:41
  • How do you submit the form data to the ControllerBean? – Javier Haro Sep 04 '15 at 09:41
  • Also, you forgot the slash charecter in the `inputText` component: `` – Javier Haro Sep 04 '15 at 11:43
  • Submit implementation suggestion: `` `` `` `` `` `` `` – Javier Haro Sep 04 '15 at 12:01

1 Answers1

0

You didn't close <h:inputText> and with the given code using @ViewScoped I am able to save empty values. Add to commandButton action attribute with method printing out every element of the list and see if it really does not change.

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

After deleting content of 3rd element:

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
Geinmachi
  • 1,251
  • 1
  • 8
  • 20