2011-10-17 30 views
0

我正在使用JSF 1.1。我有一個JSF從jsp的dataTable訪問「var」屬性值

.... 
    <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} JSP頁面中有一個名爲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

您正試圖混合使用兩種完全不同的編程範例。一般來說,你根本不應該使用'<%/ * code * /%>'scriptlets。 – McDowell

回答

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將無法使用在執行嵌入式代碼的時間。這也不是推薦的做法。