简体   繁体   English

将属性设置为jsf Managed-Bean

[英]Set properties to jsf managed-bean

Have following first .jsf: 首先具有以下.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: 具有以下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 定义为product托管Bean名称
iteration goes this way(shoppingCart.jsf): 迭代是这样的(shoppingCart.jsf):
h:dataTable value="#{prod}">
so it means that this iteration is not connected with bean named product anyhow 因此,这意味着该迭代不会与名为product bean连接

How to set properties prod.price,prod.weight,prod.count to real managed bean properties: 如何将属性prod.price,prod.weight,prod.count为真正的托管bean属性:

product.price,product.weight,product.size

There are two problems: 有两个问题:

  1. You aren't setting a specific prod in the session scoped bean. 您没有在会话范围的bean中设置特定的prod 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. 顺便说一句, managed-property声明仅在父bean的创建期间将新的/空bean设置为属性。 This is not necessarily the same prod instance as you have in the ui:repeat . 这不一定 ui:repeat中的prod实例相同 You can just remove the #{product} bean from your faces-config.xml . 您只需从faces-config.xml删除#{product} bean。

  2. The h:dataTable doesn't make any sense here. h:dataTable在这里没有任何意义。 You need h:panelGrid here. 您在这里需要h:panelGrid

     <h:panelGrid columns="3"> <h:outputText value="#{showProducts.product.name}" /> <h:outputText value="#{showProducts.product.price}" /> <h:outputText value="#{showProducts.product.count}" /> </h:panelGrid> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM