关于jsf 2:在onclick事件之后,将值从h:outputLink传递给JSF

Pass a value from h:outputLink to JSF after onclick event

h:outputLink上的onclick事件之后,我需要将一个整数传递给JSF支持bean。

重要说明:由于无法防止h:outputlink的默认onclick行为,因此无法使用f:param将值作为请求参数传递给导航页面。 该控件将转到javascript函数,而不是导航到href属性定义的页面。

将Primefaces 3.0M3快照与JSF 2.0结合使用

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<h:outputLink id="#{item.id}" value="/itemDetails.xhtml" class="itemLink">
      #{item.name}
</h:outputLink>



$(".itemLink").click(function(event) {
  showDetailsInDialog();// show the item details in dialog
  event.preventDefault();// prevent the behaviour provided by href
});



<h:form>
    <p:remoteCommand name="showDetailsInDialog" update="itemDetailsPanel" oncomplete="itemDetailsDialog.show()">
        <f:setPropertyActionListener value="....id of the item...." target="#{itemsList.selectedItem}"/>
    </p:remoteCommand>
</h:form>

我有一个可重用的dialog,它显示从项目列表中选择的项目的详细信息。 为此,当单击元素的h:outputLink时,需要将该项目的ID传递给JSF以在dialog中呈现适当的内容。

如上所示,如果可以在remotecommand中获取项目的ID,则可以通过setPropertyActionListener将其传递到适当的支持bean


我认为您应该使用p:commandLink而不是h:outputLink,如下所示-

查看-

1
2
3
4
5
<h:form>
    <p:commandLink value="#{item.name}" action="#{myBean.fetchItem()}" update="detailPanel" oncomplete="detailDlg.show();">
        <f:setPropertyActionListener target="#{myBean.itemId}" value="#{item.id}"/>
    </p:commandLink>
</h:form>

豆 -

1
2
3
4
5
6
7
8
9
10
11
12
13
@ManagedBean
@ViewScoped
public class MyBean {

    @ManagedProperty(value="#{itemStore}")
    private ItemStore itemStore;

    private int itemId; //getter/setter
    private Item item;  //getter/setter

    public void fetchItem() {
        this.item = this.itemStore.getItemWithId(this.itemId);
    }

更新:

您可以按照以下方式使用JQuery来做到这一点-

1
2
3
4
5
6
7
8
9
10
11
12
13
    jQuery(document).ready(function() {
            jQuery(".itemLink").click(function(event){
                jQuery("#itemIdHI").attr("value", jQuery(this).attr("id"));
                remCom();
                event.preventDefault();
            });
        });


<h:form prependId="false">
    <h:inputHidden id="itemIdHI" value="#{myBean.itemId}"/>
    <p:remoteCommand name="remCom" action="#{myBean.axnMethod()}" process="itemIdHI" update="detailPanel" oncomplete="detailDlg.show()"/>
</h:form>


看一下这个。我和您有同样的问题,但是在阅读了BalusC的链接后我解决了。

简而言之,您正在谈论的内容是:

f:attribute:与h:commandLink和h:commandButton标记一起,您还可以使用actionListener属性触发支持Bean的方法。这样,您还可以使用f:attribute标记动态传递参数。这是一个例子:

1
2
3
4
5
6
7
8
9
10
11
<h:form>
    <h:commandLink value="Click here" actionListener="#{myBean.action}">
        <f:attribute name="attributeName1" value="attributeValue1" />
        <f:attribute name="attributeName2" value="attributeValue2" />
    </h:commandLink>

    <h:commandButton value="Press here" actionListener="#{myBean.action}">
        <f:attribute name="attributeName1" value="attributeValue1" />
        <f:attribute name="attributeName2" value="attributeValue2" />
    </h:commandButton>
</h:form>

可以使用父UI组件的getAttributes()来检索这些属性,而该属性又可以通过actionListener传递的ActionEvent来检索。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package mypackage;

import javax.faces.event.ActionEvent;

import net.balusc.util.FacesUtil;

public class MyBean {

    // Actions -----------------------------------------------------------------------------------

    public void action(ActionEvent event) {
        String attributeName1 = FacesUtil.getActionAttribute(event,"attributeName1");
        String attributeName2 = FacesUtil.getActionAttribute(event,"attributeName2");

        System.out.println("attributeName1:" + attributeName1);
        System.out.println("attributeName1:" + attributeName1);
    }

}

package net.balusc.util;

import javax.faces.event.ActionEvent;

public class FacesUtil {

    // Getters -----------------------------------------------------------------------------------

    public static String getActionAttribute(ActionEvent event, String name) {
        return (String) event.getComponent().getAttributes().get(name);
    }

}

变量attributeName1和attributeName2现在应分别包含值attributeValue1和attributeValue2。

请注意,每个属性名称都应该是唯一的,并且不应覆盖任何默认的组件属性,例如" id"," name"," value"," binding"," rendered"等。