将属性设置为 jsf 托管 bean

发布于 2024-10-05 21:42:05 字数 2053 浏览 6 评论 0原文

首先有以下 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-10-12 21:42:06

有两个问题:

  1. 您没有在会话作用域 bean 中设置特定的prod。你应该这样做。

    
        
    
    

    顺便说一句,托管属性声明仅在父 bean 的创建期间将新/空 bean 设置为属性。这不一定与 ui:repeat 中的 相同 prod 实例相同。您只需从 faces-config.xml 中删除 #{product} bean 即可。

  2. h:dataTable 在这里没有任何意义。此处需要 h:panelGrid

    
        >
        >
        
    
    

There are two problems:

  1. 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 same prod instance as you have in the ui:repeat. You can just remove the #{product} bean from your faces-config.xml.

  2. 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>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文