0

我有一个 h:datatable 示例,用于在用户单击表格行时打开一个新页面。不幸的是,此示例使用 http 标头将参数传递给打开的页面。我的问题是这可以实现,但将参数传递到后台而不是标题?

这是完整的源代码

这是 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.xhml 的托管 bean 并获取我想要传递的值?但也许我需要更改javascript?

4

1 回答 1

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 (managed 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

于 2012-05-11T13:54:13.283 回答