如何使用链接调用bean操作方法? onclick不起作用 [英] How to invoke a bean action method using a link? The onclick does not work

查看:166
本文介绍了如何使用链接调用bean操作方法? onclick不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施一个用户名列表,可以通过点击 UP DOWN 链接重新排列。

I'm trying to implement a list of users names which can be rearranged by clicking on UP or DOWN links.

<ul>
    <ui:repeat var="user" value="#{cc.attrs.value}">
        <li>
            #{user.name}
            <h:link outcome = "user" value = "left" onclick="#{accountController.moveDown}">
                <f:param name="id" value = "${user.id}" />
            </h:link>
        </li>

    </ui:repeat>
</ul>

这里的问题是我似乎没有正确使用onclick属性。这样做的正确方法是什么?

The problem here is that it seems that I'm not using the onclick attribute correctly. What is the proper way for doing this?

编辑:根据您的建议我放置了所有内容表格中的链接:

Following your advices I placed all the links in a form:

<h:form>
        <ui:repeat value="#{cc.attrs.value}" var = "user">
        <div class = "user">
            <h:commandLink id = "Link1" value = "Up" binding = "#{accountController.ommandLink}" action = "#{accountController.moveUserUp}">
                <f:attribute name = "userId" value = "#{user.id}" />
            </h:commandLink>
            <h:commandLink id = "Link2" value = "Down" binding = "#{accountController.commandLink}" action = "#{accountController.moveUserDown}">
                <f:attribute name = "userId" value = "#{user.id}" />
            </h:commandLink>
            <h:commandLink id = "Link3" value = "Delete" binding = "#{accountController.commandLink}" action = "#{accountController.deleteUser}">
                <f:attribute name = "userId" value = "#{user.id}" />
            </h:commandLink>
    </div>
</h:form>

受管Bean:

private UIComponent commandLink;

public void moveUserUp(){
    Integer userId = (Integer)commandLink.getAttributes().get("userId");
    System.out.println("MOVE TAB LEFT :" + userId);
}

public void moveUserDown(){
    Integer userId = (Integer)commandLink.getAttributes().get("userId");
    System.out.println("MOVE TAB RIGHT: " + userId);
}

public void deleteUser(){
    Integer userId = (Integer)commandLink.getAttributes().get("userId");
    System.out.println("DELETE TAB: " + userId);
}

public UIComponent getCommandLink() {
    return commandLink;
}

public void setCommandLink(UIComponent commandLink) {
    this.commandLink = commandLink;
}

命令链接和托管bean之间的通信正在工作,但在UI中只显示最后一个commandLink(关闭操作)。

The communication between the command Link and the managed bean is working but in the UI only the last commandLink (close action) is displayed.

推荐答案

为了在点击链接时调用bean操作方法,你需要< h:commandLink> 。这必须包含在< h:form> 中。

In order to invoke a bean action method on click of a link, you need <h:commandLink>. This must be enclosed in a <h:form>.

<h:form>
    <h:commandLink ... action="#{bean.action}" />
</h:form>

public String action() {
    // ...

    return "/other.xhtml";
}

在JSF中,只有属性将EL表达式解释为 MethodExpression 可以使用宣布行动方法。所有其他属性都被解释为 ValueExpression ,当JSF生成HTML输出时,它们会立即执行。这包含 onclick 属性,其值实际上应代表JavaScript函数。

In JSF, only the attributes which interpret the EL expression as a MethodExpression can be used to declare action methods. All other attributes are interpreted as ValueExpression and they are immediately executed when the HTML output is generated by JSF. This covers the onclick attribute, whose value should actually represent a JavaScript function.

如果您确实想要使用获取GET链接,然后将操作方法​​移动到目标页面中的< f:viewAction> 。这将在目标页面的页面加载时调用。

In case you actually want to use a GET link, then move the action method to a <f:viewAction> in the target page. This will be invoked on page load of the target page.

<h:link ... outcome="/other.xhtml" />

<f:metadata>
    <f:viewAction action="#{bean.onload}" />
</f:metadata>

public void onload() {
    // ...
}



< h3>参见:


  • 我应该何时使用h:outputLink而不是h:commandLink?

  • 如何发送表单输入值并在JSF bean中调用方法

  • 如何在页面加载时处理支持bean中的GET查询字符串URL参数?

  • 如何在JSF中导航?如何使URL反映当前页面(而不是之前的页面)

  • See also:

    • When should I use h:outputLink instead of h:commandLink?
    • How to send form input values and invoke a method in JSF bean
    • How do I process GET query string URL parameters in backing bean on page load?
    • How to navigate in JSF? How to make URL reflect current page (and not previous one)

    • 根据您的建议,我将所有链接放在表格中

      命令之间的通信链接托管bean正在运行,但在UI中只显示最后一个commandLink(关闭操作)。

      你应该不将多个物理上不同的组件绑定到同一个bean属性。传递参数的< f:attribute> 也是hacky,在JSF2中不再需要。假设您正在使用Servlet 3.0 / EL 2.2容器(您的问题历史记录确认您正在使用Glassfish 3),而是直接将参数作为方法参数传递:

      You should not bind multiple physically different components to one and same bean property. Also the <f:attribute> to pass arguments is hacky and not necessary anymore in JSF2. Assuming that you're using a Servlet 3.0 / EL 2.2 container (your question history confirms that you're using Glassfish 3), rather just pass the argument as method argument directly:

      <h:commandLink id="Link1" value="Up" action="#{accountController.moveUserUp(user)}" />
      <h:commandLink id="Link2" value="Down" action="#{accountController.moveUserDown(user)}" />
      <h:commandLink id="Link3" value="Delete" action="#{accountController.deleteUser(user)}" />
      

      with

      public void moveUserUp(User user) {
          // ...
      }
      
      public void moveUserDown(User user) {
          // ...
      }
      
      public void deleteUser(User user) {
          // ...
      }
      



      参见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆