Write a map property in h: inputText in h: dataTable

I want to add input text where the user can edit the quantity in h:dataTable

<h:dataTable id="checkout_table" value="#{cartController.cart.entrySet()}"
    var="item" >
    <h:column>
        <f:facet name="header">Movie</f:facet>
        #{item.key.title} - #{item.key.getAvailable()}
    </h:column>
    <h:column>
        <f:facet name="header">Quantity</f:facet>
        <h:inputText id="quantity"
            value="#{item.value}" redisplay="true"
            converterMessage="Please provide an integer" required="true"
            requiredMessage="Please enter a quantity">
            <!-- <f:validateLongRange minimum="1"
                maximum="#{item.key.getAvailable()}" />
                    this validation must be performed on set-->
            <f:ajax event="blur" render="quantityMessage" />
        </h:inputText>
    </h:column>
    <h:column><h:commandButton value="edit"
        action="#{cartController.addMovieToCart(item.key, item.value)}" />
    </h:column>
    <h:column><h:commandButton value="delete"
        action="#{cartController.removeMovie(item.key)}" />
    </h:column>
    <h:column><h:message id="quantityMessage" for="quantity" /></h:column>
</h:dataTable>

As of now, the exception is thrown The class 'java.util.LinkedHashMap$Entry' does not have a writable property 'value'. I read various things on binding, but I can’t wrap my head around me. What should I do to determine the getter and setter for this input file? Is there any other component that I could use?

If the check can be handled as well, that would be appreciated!

EDIT : after @BalusC answer I managed to read and write to the map - with one caveat - I get CCE java.lang.String cannot be cast to java.lang.Integerin bean

private Map<Movie, Integer> cart = new LinkedHashMap<>();

public String addMovieToCart(Movie movie, Integer quantity) {
    if (quantity > movie.getAvailable()) {
        String message = "Available copies are " + movie.getAvailable();
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
        return null;
    }
    Integer ordered = cart.get(movie); // CCE here !
    // etc
}

This was finally decided:

<h:inputText id="quantity"
    value="#{cartController.cart[item.key]}"
    converterMessage="Please provide an integer" required="true"
    requiredMessage="Please enter a quantity" converter="javax.faces.Integer">

Pay attention to the converter - apparently, JSF converts map elements to String? Must explicitly convert to integer

, (- /)

+4
1

Map EL, #{map[entry.key]}, . #{entry.value} , , .

, :

<h:dataTable value="#{cartController.cart.entrySet()}" var="item">
    ...
    <h:inputText ... value="#{cartController.cart[item.key]}" />

, : ui: repeat

, redisplay <h:inputText>. , <h:inputSecret>?

+10

All Articles