如何使用链接调用bean操作方法?onclick不起作用

TGM*_*TGM 5 jsf onclick hyperlink actionmethod jsf-2

我正在尝试实现一个用户名列表,可以通过单击UPDOWN链接重新排列.

<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>
Run Code Online (Sandbox Code Playgroud)

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


编辑:根据您的建议,我将所有链接放在一个表单中:

<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>
Run Code Online (Sandbox Code Playgroud)

托管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;
}
Run Code Online (Sandbox Code Playgroud)

命令Link和托管bean之间的通信正在运行,但在UI中仅显示最后一个commandLink(关闭操作).

Bal*_*usC 12

要在点击链接时调用bean操作方法,您需要<h:commandLink>.这必须包含在<h:form>.

<h:form>
    <h:commandLink ... action="#{bean.action}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

public String action() {
    // ...

    return "/other.xhtml";
}
Run Code Online (Sandbox Code Playgroud)

在JSF中,只有将EL表达式解释为a 的属性MethodExpression才能用于声明操作方法.所有其他属性都被解释为ValueExpression,当JSF生成HTML输出时,它们会立即执行.这涵盖了onclick属性,其值实际上应代表JavaScript函数.

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

<h:link ... outcome="/other.xhtml" />
Run Code Online (Sandbox Code Playgroud)

<f:metadata>
    <f:viewAction action="#{bean.onload}" />
</f:metadata>
Run Code Online (Sandbox Code Playgroud)

public void onload() {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

也可以看看:


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

命令Link和托管bean之间的通信正在运行,但在UI中仅显示最后一个commandLink(关闭操作).

您不应该将多个物理上不同的组件绑定到同一个bean属性.另外,<f:attribute>来传递参数是哈克并没有必要再在JSF2.假设您正在使用Servlet 3.0/EL 2.2容器(您的问题历史证实您正在使用Glassfish 3),而是直接将参数作为方法参数传递:

<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)}" />
Run Code Online (Sandbox Code Playgroud)

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

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

public void deleteUser(User user) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

也可以看看:


归档时间:

查看次数:

31295 次

最近记录:

9 年,4 月 前