开发者

Set properties to jsf managed-bean

Have following first .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>

Have following 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开发者_Python百科" action="#{showProducts.persistOrder}">
</h:form>

Faces-config:

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

The problem:

managed bean name defined as product

iteration goes this way(shoppingCart.jsf):

h:dataTable value="#{prod}">

so it means that this iteration is not connected with bean named product anyhow

How to set properties prod.price,prod.weight,prod.count to real managed bean properties:

product.price,product.weight,product.size


There are two problems:

  1. You aren't setting a specific prod in the session scoped bean. You should do this.

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

    By the way, the managed-property declaration only sets an new/empty bean as property during parent bean's ceration. This is not necessarily the same prod instance as you have in the ui:repeat. You can just remove the #{product} bean from your faces-config.xml.

  2. The h:dataTable doesn't make any sense here. You need h:panelGrid here.

    <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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜