将属性设置为jsf Managed-Bean [英] Set properties to jsf managed-bean
问题描述
遵循第一个.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:
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>
面孔配置:
<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连接
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
如何将属性prod.price,prod.weight,prod.count
设置为真正的托管Bean属性:
How to set properties prod.price,prod.weight,prod.count
to real managed bean properties:
product.price,product.weight,product.size
推荐答案
有两个问题:
-
您没有在会话范围的bean中设置特定的
prod
.您应该这样做.
<h:commandButton value="Buy" action="shoppingCart">
<f:setPropertyActionListener target="#{showProducts.product}" value="#{prod}" />
</h:commandButton>
顺便说一句,managed-property
声明仅在父bean的创建期间将一个新的/空bean设置为属性.这不一定与ui:repeat
中的相同 prod
实例相同.您只需从faces-config.xml
中删除#{product}
bean.
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
.
h:dataTable
在这里没有任何意义.您需要在这里h:panelGrid
.
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>
这篇关于将属性设置为jsf Managed-Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!