代码之家  ›  专栏  ›  技术社区  ›  sergionni

将属性设置为jsf托管bean

  •  2
  • sergionni  · 技术社区  · 14 年前

    首先有以下.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>
    

    面配置:

        <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}">
    所以这意味着这个迭代与名为 产品 无论如何

    如何设置属性 prod.price,prod.weight,prod.count 对于真正的托管bean属性:

    product.price,product.weight,product.size
    
    1 回复  |  直到 14 年前
        1
  •  5
  •   BalusC    14 年前

    有两个问题:

    1. 你没有设定一个特定的 prod 在会话作用域bean中。你应该这么做。

      <h:commandButton value="Buy" action="shoppingCart">
          <f:setPropertyActionListener target="#{showProducts.product}" value="#{prod}" />
      </h:commandButton>
      

      顺便说一下 managed-property 声明仅在父bean执行期间将新的/空的bean设置为属性。这不一定是 相同的 产品 例如你在 ui:repeat . 你可以把 #{product} 你的豆子 faces-config.xml .

    2. 这个 h:dataTable 在这里没有任何意义。你需要 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>