Set properties to jsf managed-bean

£可爱£侵袭症+ 提交于 2019-12-21 21:43:34

问题


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

回答1:


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>
    


来源:https://stackoverflow.com/questions/4312085/set-properties-to-jsf-managed-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!