属性以JSF托管bean

问题描述:

有下列第一.jsf:属性以JSF托管bean

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

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

如何设置属性prod.price,prod.weight,prod.count实际管理bean属性连接:

product.price,product.weight,product.size 

有两个问题:

  1. 你AREN” t在会话作用域bean中设置特定的prod。你应该做这个。

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

    顺便说一句,在managed-property声明只设置父bean的细齿过程中新/空豆财产。这不一定是相同prod实例,因为您在ui:repeat中有。您可以从faces-config.xml中删除#{product}豆。

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

BalusC,非常感谢。它工作。我甚至不知道 sergionni 2010-11-30 13:48:07