JSF从jsp的dataTable访问“var”属性值

时间:2011-10-17 09:45:01

标签: jsf

我正在使用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的属性。如果它的值是“待定”,那么我想显示<h:commandButton>,否则我什么都不显示。

如何在JSP scriptlet代码中获取#{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>

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

1 个答案:

答案 0 :(得分:1)

您应该使用“已渲染”属性:

  <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 var将不可用。这也不是推荐的做法。