【问题标题】:JSF unique input bean for each item in a loop循环中每个项目的 JSF 唯一输入 bean
【发布时间】:2012-05-25 08:38:08
【问题描述】:

我想遍历一个元素列表,并为每个元素显示一个表单。在那种形式中,我希望能够为循环的每个元素填充一个不同的 bean。这是我到目前为止得到的,但它当然不起作用;)

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

我的 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;
    }

}

PriceRange 是一个 POJO,包含最小、最大、价格为 String

调用页面的控制器用与ProductPositions 一样多的PriceRanges 填充PriceRangeBean,以便在列表中准备好创建一个“新”PriceRange 对象。但是输入似乎没有到达支持 bean。

任何想法我做错了什么?

谢谢!

【问题讨论】:

    标签: java jsf jsf-2


    【解决方案1】:

    我认为您不能对&lt;h:inputText 中的value 属性使用这种EL 表达式。

    表达式:

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

    被(至少)评估了两次。 一旦需要在“应用请求值”中设置该值。第二次是在“渲染响应阶段”。

    当设置值时(在应用请求值阶段),EL 将表达式评估为lvalue,这似乎具有有限的功能。

    来自规范(JavaServer Pages 2.1 Expression Language Specification)

    左值只能由单个变量(例如 ${name})或 财产决议。

    您可能想查看同一规范中的第 1.2.1.1 节。

    【讨论】:

    • 听起来很合理。害怕它会是这样的......关于如何实现我的任务的任何想法,即拥有动态数量的 bean 来保存动态数量的表单的输入? ;)
    • 我们有一个案例,我们在列表/数组中只有固定数量的对象。为了简单起见,我们只是踢出列表/数组并直接使用对象。所以,我真的没有手头的想法。对不起。
    猜你喜欢
    • 2022-11-25
    • 2012-09-04
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多