2012-05-07 39 views
0

我有一個h:datatable的例子,用於在用戶單擊表格行時打開新頁面。不幸的是,這個例子使用http頭將參數傳遞給打開的頁面。我的問題是這可以實現,但將參數傳遞到背景而不是頭部?如何點擊行並使用託管bean傳遞變量

這是完整的源code

這是JSF頁面:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <head> 
     <title>test</title> 
     <script type="text/javascript"> 
      function addOnclickToDatatableRows() { 
       //gets all the generated rows in the html table 
       var trs = document.getElementById('myForm:dataTable').getElementsByTagName('tbody')[0] 
       .getElementsByTagName('tr'); 
       //on every row, add onclick function (this is what you're looking for) 
       for (var i = 0; trs.length > i; i++) { 
        trs[i].onclick = new Function("rowOnclick(this)"); 
       } 
      } 

      function rowOnclick(tr) { 
       //    var childNodes = tr.childNodes; 
       //    for(var i = 0; childNodes.length > i; i++) { 
       //      
       //    } 

       var elements = tr.cells[0].childNodes; 
       for(var i = 0; elements.length > i; i++) { 
        if ((typeof elements[i].id !== "undefined") &amp;&amp; 
        (elements[i].id.indexOf("lnkHidden") > -1)) { 
         //opne in a new window// window.open(elements[i].href); 
         location.href=elements[i].href 
         break; 
        } 
       } 
       return false; 
      } 
     </script> 
    </head> 
    <body onload="addOnclickToDatatableRows();"> 
     <h:form id="myForm"> 
      <h1>Click on table row example</h1> 
      <h:dataTable id="dataTable" var="data" value="#{datatableBean.lstData}" border="1"> 
       <h:column> 
        <f:facet name="header"> 
         <h:outputText value="ID" /> 
        </f:facet> 
        <h:outputText value="#{data.id}" /> 
        <h:outputLink id="lnkHidden" value="AnotherPage.xhtml" 
            style="display:none"> 
         <f:param name="id" value="#{data.id}" /> 
        </h:outputLink> 
       </h:column> 
       <h:column> 
        <f:facet name="header"> 
         <h:outputText value="Value1" /> 
        </f:facet> 
        <h:outputText value="#{data.value}" /> 
       </h:column> 
       <h:column> 
        <f:facet name="header"> 
         <h:outputText value="Value2" /> 
        </f:facet> 
        <h:outputText value="#{data.value}" /> 
       </h:column> 
      </h:dataTable> 
     </h:form> 
    </body> 
</html> 

這是管理的bean:

package edu.home; 

    import edu.home.model.Data; 
    import java.util.ArrayList; 
    import java.util.List; 
    import javax.faces.bean.ManagedBean; 
    import javax.faces.bean.ViewScoped; 

    @ManagedBean 
    @ViewScoped 
    public class DatatableBean { 

     private List<Data> lstData; 
     /** 
     * Creates a new instance of datatableBean 
     */ 
     public DatatableBean() { 
      lstData = new ArrayList<Data>(); 
      lstData.add(new Data(1, "Hello World")); 
      lstData.add(new Data(2, "Hello 123")); 
      lstData.add(new Data(3, "Hello abv")); 
      lstData.add(new Data(4, "Hello qaz")); 
     } 

     /** 
     * @return the lstData 
     */ 
     public List<Data> getLstData() { 
      return lstData; 
     } 

     /** 
     * @param lstData the lstData to set 
     */ 
     public void setLstData(List<Data> lstData) { 
      this.lstData = lstData; 
     } 
    } 

This is the class Data: 

package edu.home.model; 

public class Data { 

    private int id; 
    private String value; 

    public Data(int id, String value) { 
     this.id = id; 
     this.value = value; 
    } 
    /** 
    * @return the id 
    */ 
    public int getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(int id) { 
     this.id = id; 
    } 

    /** 
    * @return the value 
    */ 
    public String getValue() { 
     return value; 
    } 

    /** 
    * @param value the value to set 
    */ 
    public void setValue(String value) { 
     this.value = value; 
    } 
} 

我敢肯定,這是可能的,但我可以用適當的方式將論點傳遞給我們。

編輯 我明白h:outputLink必須這樣改變:

<h:column> 
    <f:facet name="header"> 
     <h:outputText value="ID" /> 
    </f:facet> 
    <h:outputText value="#{data.id}" /> 
    <h:commandLink id="lnkHidden" value="AnotherPage.xhtml" 
        style="display:none"> 
     <f:param name="id" value="#{data.id}" /> 
    </h:commandLink> 
</h:column> 

但我不明白的託管Bean如何必須改變。也許我想如果AnotherPage.xhtml可以訪問DataTable.x hml的託管bean並獲取我想要傳遞的值?但也許那麼我需要改變javaScript?

+2

我已經回答了你,當你問這個在[較早的帖子(HTTP://計算器你應該把''改成''或者是一個''在你的動作方法中執行邏輯和導航。在尋求幫助之前,您應該嘗試一些代碼。 –

+0

@Luiggi門多薩發佈更新 –

+1

Peter您應該瞭解標籤組件如何在JSF 2中工作。我建議你閱讀[JSF 2 commandbutton](http://www.mkyong.com/jsf2/jsf-2-button-and-commandbutton-example/)和[action for commandButton](http://stackoverflow.com/ a/3807808/1065197),最後一個由BalusC製作(JSF專家) –

回答

2

您嘗試自己使用並渲染您的表格。

這是我的例子。根據您的要求修改它。我用JSF 2.0

<h:form id="nextPage"> 

    <table> 
     <ui:repeat var="row" value="#{shoppingCartMB.shoppingItems}"> 
      <tr onclick="clickedMe('#{row.productId}');"> 
       <td>#{row.productId}</td> 
      </tr> 
     </ui:repeat> 
    </table> 

    <script> 

     function clickedMe(id) 
     { 

      // Please modify following URL base on ur requirment.. 
      // Following is just for sample.. 
      location.href="#{request.contextPath}/product/" + id; 
     } 

    </script> 
</h:form> 

下面是一些非常-config.xml中的

<url-mapping id="productMB-loadProductDetail"> 
    <pattern value="/product/#{ productMB.productId }" /> 
    <view-id value="/pages/product-detail.jsf" />  
    <action>#{productMB.loadProductDetail}</action> 
</url-mapping> 

有內部productMB(託管Bean)的loadProductDetail(),你把另一重定向(..)。

事情是這樣的..

response.sendRedirect(request.getContextPath() + "/product-info"); 
再次漂亮配置

..

你必須把配置於上述網址..

<url-mapping id="product-info"> 
    <pattern value="/product-info" /> 
    <view-id value="/pages/product-info.jsf" /> 
</url-mapping> 

在我的應用程序,我做了像那樣隱藏一些URL。

希望得到這個幫助。

這裏是我的示例源代碼和我的理解..我上傳的zip文件到4shared.com http://www.4shared.com/zip/ctPZ4Dbj/URL_Hidding.html

+0

OP提到他不希望URL中的行標識符。 – BalusC

+0

請嘗試使用PrettyFace ..您可能可以隱藏該網址。做下面這樣的水漬..我編輯我的舊帖子 –

+0

這不會在沒有通過產品ID的GET工作。 – BalusC

相關問題