2

有以下第一个.jsf:

<ui:repeat var="prod" value="#{showProducts.decoys}">
     <h:form>
       {prod.price} 
       {prod.weight} 
       {prod.size} >
    <h:commandButton value="Buy" action="shoppingCart"/>
    </h:form>
</ui:repeat>

有以下shoppingCart.jsf:

<h:form>
 <h:dataTable value="#{prod}">
  <h:column>
   #{prod.name}<br/>
  </h:column>
  <h:column>
   #{prod.price}<br/>
  </h:column>
  <h:column>        
   <h:inputText value="#{prod.count}" size="3"/>
  </h:column>
</h:dataTable>  
<h:inputText value="#{order.phone}"/><br/>
<h:inputText value="#{order.mail}"><br/>
<h:inputText value="#{order.city}"/><br/>
<h:commandButton value="Order" action="#{showProducts.persistOrder}">
</h:form>

面孔配置:

    <managed-bean>
        <managed-bean-name>showProducts</managed-bean-name>
            <managed-bean-class>main.ShowProducts</managed-bean-class>
            <managed-bean-scope>session</managed-bean-scope>
...
            <managed-property>
               <property-name>product</property-name>
               <value>#{product}</value>
            </managed-property>
        </managed-bean>

    <managed-bean>
        <managed-bean-name>product</managed-bean-name>
        <managed-bean-class>main.Product</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
...

问题:
定义为product
迭代的托管bean名称是这样的(shoppingCart.jsf):
h:dataTable value="#{prod}">
所以这意味着这个迭代product无论如何都没有与命名的bean连接

如何将属性设置prod.price,prod.weight,prod.count为真正的托管 bean 属性:

product.price,product.weight,product.size
4

1 回答 1

5

有两个问题:

  1. prod您没有在会话范围的 bean 中设置特定的。你应该做这个。

    <h:commandButton value="Buy" action="shoppingCart">
        <f:setPropertyActionListener target="#{showProducts.product}" value="#{prod}" />
    </h:commandButton>
    

    顺便说一句,该managed-property声明仅在父 bean 的认证期间将新的/空的 bean 设置为属性。这不一定 prod您在ui:repeat. 你可以#{product}从你的faces-config.xml.

  2. 在这里h:dataTable没有任何意义。你需要h:panelGrid在这里。

    <h:panelGrid columns="3">
        <h:outputText value="#{showProducts.product.name}" />
        <h:outputText value="#{showProducts.product.price}" />
        <h:outputText value="#{showProducts.product.count}" />
    </h:panelGrid>
    
于 2010-11-30T12:24:55.347 回答