将属性设置为 jsf 托管 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:
<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>
...
问题:
托管 Bean 名称定义为 product
迭代是这样的(shoppingCart.jsf):h:dataTable value="#{prod}">
因此,这意味着此迭代无论如何都不会与名为 product
的 bean 连接
如何将属性 prod.price,prod.weight,prod.count
设置为真正的托管 bean 属性:< br>
product.price,product.weight,product.size
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个问题:
您没有在会话作用域 bean 中设置特定的
prod
。你应该这样做。顺便说一句,
托管属性
声明仅在父 bean 的创建期间将新/空 bean 设置为属性。这不一定与ui:repeat
中的 相同prod
实例相同。您只需从faces-config.xml
中删除#{product}
bean 即可。h:dataTable
在这里没有任何意义。此处需要h:panelGrid
。There are two problems:
You aren't setting a specific
prod
in the session scoped bean. You should do this.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.