JSF 从 jsp 访问 dataTable 中的 "var" 属性值



我使用JSF 1.1。我有一个带有

的JSP页面
    ....
    <t:dataTable id="data"
       styleClass="scrollerTable"
       headerClass="standardTable_Header"
       footerClass="standardTable_Header"
       columnClasses="col1,col2,col3"
       var="product"
       value="#{beanProduct.listOfProducts}"
       preserveDataModel="false"
       rows="10"
    >
    ....

#{product}有一个名为state的属性。如果它的值是"pending",那么我想显示一个<h:commandButton>,否则我什么都不显示。

如何在JSP脚本代码中获得#{product.state}的值?

我试了下一个:

    ....
    <h:column>
       <f:facet name="header">
             <h:outputText value="#{messages['list']}" />
       </f:facet>
       <%
           // ----> In this point, I want know the value of "product.state"
       FacesContext context = FacesContext.getCurrentInstance();  
       Application app = context.getApplication();  
       ValueBinding binding = app.createValueBinding("#{product.state}"); 
       Object valueOfProduct = binding.getValue(context);
       String stateProduct = (String)valueOfProduct;
       if (stateProduct.equals("pending")) {
       %>
       <h:panelGrid>
        <h:commandButton value="Send" actionListener="#{beanProduct.Item}" action="#{beanProduct.sending">
        <f:attribute name="idProduct" value="#{product.id}" />
            </h:commandButton>
           </h:panelGrid>
           <%
           }
           else {
           %>
           // Do nothing.
           <%
       }
       %>
    </h:column>
</t:dataTable>

但是它不起作用。我怎样才能做到这一点呢?

你应该使用"rendered"属性:

  <h:column>
     <f:facet name="header">
       <h:outputText value="#{messages['list']}" />
     </f:facet>
     <h:commandButton rendered="#{product.pending}" value="Send" 
                      actionListener="#{beanProduct.Item}" 
                      action="#{beanProduct.sending">
        <f:attribute name="idProduct" value="#{product.id}" />
     </h:commandButton>
  </h:column>
像这样混合JSF和JSP是行不通的,因为在执行嵌入代码时dataTable变量是不可用的。

最新更新