问题
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:
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 sameprod
instance as you have in theui:repeat
. You can just remove the#{product}
bean from yourfaces-config.xml
.The
h:dataTable
doesn't make any sense here. You needh: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