本文介绍了在onclick事件之后,将值从h:outputLink传递给JSF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to pass an integer to a JSF backing bean after onclick event on h:outputLink.

重要提示:我无法使用f:param将值作为请求参数传递给导航页面,因为这是在防止h:outputlink的默认onclick行为. 该控件而不是导航到href属性定义的页面,而是使用了javascript函数.

Important : I cannot use f:param to pass value as request parameters to the naviagating page as I am preventing default onclick behaviour of h:outputlink. The control instead of navigating to page defined by href attribute, goes to a javascript function.

在JSF 2.0中使用Primefaces 3.0M3快照

Using Primefaces 3.0M3 snapshot with JSF 2.0

我的代码如下:

<h:outputLink id="#{item.id}" value="/itemDetails.xhtml" class="itemLink" >
      #{item.name}
</h:outputLink>


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


<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中呈现适当的内容.

I have a reusable dialog that displays the details of item selected from a itemslist. For this when the h:outputLink for an element is clicked the id of that item needs to be passed to JSF to render appropriate content in dialog.

如上所示,如果可以在remotecommand中获取项目的ID,则可以通过setPropertyActionListener

As shown above If I can get the id of item in remotecommand, I can pass it to appropriate backing bean through setPropertyActionListener

推荐答案

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

I think you should use p:commandLink instead of h:outputLink as follows -

查看-

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

Bean-

@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来做到这一点-

You can do that by using JQuery as follows -

<script>
    jQuery(document).ready(function() {
            jQuery(".itemLink").click(function(event){
                jQuery("#itemIdHI").attr("value", jQuery(this).attr("id"));
                remCom();
                event.preventDefault();
            });
        });
</script>

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

这篇关于在onclick事件之后,将值从h:outputLink传递给JSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:17