Pete
Pete

Reputation: 10938

JSF unique input bean for each item in a loop

I want to iterate over a list of elements and for each element I want to display a form. In that form I want to be able to fill a bean that is different for each element of the loop. Here's what I got so far but of course it doesn't work ;)

    <ui:repeat value="#{productBean.productPositions}" var="position">
        <div>position info here</div>
        <div>list price ranges</div>
        <div>
            <h5>Create new price range for product position position</h5>
            <h:form>
                Min:
                <h:inputText
                    value="#{priceRangeBean.priceRanges[productPositionRangeSearch.productPositions.indexOf(position)].min}" />
                Max:
                <h:inputText
                    value="#{priceRangeBean.priceRanges[productPositionRangeSearch.productPositions.indexOf(position)].max}" />
                price:
                <h:inputText
                    value="#{priceRangeBean.priceRanges[productPositionRangeSearch.productPositions.indexOf(position)].price}" />
                <h:commandButton value="create"
                    action="#{priceRangeController.createRange(productPositionRangeSearch.productPositions.indexOf(position))}" />
            </h:form>

        </div>
    </ui:repeat>

my PriceRangeBean:

@SessionScope
public class PriceRangeBean {

    private List<PriceRange> priceRanges = new ArrayList<PriceRange>();

    public List<PriceRange> getPriceRanges() {
        return priceRanges;
    }

    public void setPriceRanges(List<PriceRange> priceRanges) {
        this.priceRanges = priceRanges;
    }

}

with PriceRange being a POJO containing min, max, price as String

The controller calling the page fills the PriceRangeBean with as many PriceRanges as there are ProductPositions so that there is a "new" PriceRange Object in the list ready to be created. However the input doesn't seem to reach the backing bean.

Any ideas what I'm doing wrong?

Thanks!

Upvotes: 1

Views: 517

Answers (1)

Jens
Jens

Reputation: 6383

I think you can not use this kind of EL expression for the value property in the <h:inputText.

The expression:

 value="#{priceRangeBean.priceRanges[productPositionRangeSearch.productPositions.indexOf(position)].min}"

is (at least) evaluated two times. Once when the value needs to be set in the "apply request values". And a second time in the "render response phase".

When the value is set (in the apply request values phase) the expression is evaluated by the EL as an lvalue and that seems to have limited capabilities.

From the Spec (JavaServer Pages 2.1 Expression Language Specification):

an lvalue can only consist of either a single variable (e.g. ${name}) or a property resolution.

You may want to check out the section 1.2.1.1 in that same spec.

Upvotes: 1

Related Questions