【问题标题】:Action method is not called the second time第二次没有调用动作方法
【发布时间】:2015-03-17 09:52:31
【问题描述】:

我正在使用模板,在我的主模板中有一个

<ui:include src="#{navBean.content}.xhtml">

我的问题是,任何 bean 的 action 方法,都没有在上一页运行过。这是一个示例,当您单击第一页上的菜单时,会进行 ajax 调用,执行操作方法并加载页面内容。通过单击加载页面(page1)上的链接,不执行操作方法。

配置:jsf 2.2.9 |素面5.1 | Tomcat 8.0.3

模板.xhtml

<div>
    <div>
        <h:form>
            <p:slideMenu>
                <p:submenu label="Cat 1">
                    <p:menuitem value="page1" action="#{navBean.changeMenuContent('page1')}" update=":change"/>
                    <p:menuitem value="bla"/> 
                </p:submenu>
            </p:slideMenu>
        </h:form>
    </div>
    <div>
        <h:form id="change">
            <ui:include src="/#{navBean.content}.xhtml"/>
        </h:form>
    </div>
</div>

导航豆:

@ManagedBean(name = "navBean")
@RequestScoped
public class NavigationBean
{
    private String content = "index";
    public void changeMenuContent(String content)
    {
        setContent(content);
    }
    //get set...
}

page1.xhtml

<p:commandLink value="teste" action="#{navBean.changeMenuContent('page2')}" ajax="false"/>

page2只有一个&lt;p: outputLabel /&gt;,用于测试。

我试过p:commandLinkh:commandLink

谢谢!

【问题讨论】:

  • 您的收录页面(page1.xhtml)是否有表单标签?
  • 不,只有 commandLink。
  • 嗨@VitorFreitas 你解决了吗?我对 commandButton 也有同样的问题。使用 primefaces 5.0 jsf sun 2.2.4

标签: jsf primefaces facelets uiinclude


【解决方案1】:

你能检查你的浏览器是否有任何 JS 错误吗?我在尝试复制此问题时在 Firebug 中收到“TypeError:b 未定义”。它似乎与 Primefaces 5.x 版本有关。当我回滚到 4.0 版时,它工作正常。

【讨论】:

    【解决方案2】:

    尝试添加update=":change"on:

    <p:commandLink value="teste" action="#{navBean.changeMenuContent('page2')}" ajax="false" update=":change />
    

    看到changeMenuContent就是void,所以不会导致页面导航。

    所以你需要手动运行“更新”。

    另请参阅,您的 Bean 是 @RequestScope,因此每次请求都会重新创建其内容。

    所以最推荐的是将Bean更改为@SessionScope,并且:

    <p:commandLink value="teste" actionListener="#{navBean.changeMenuContent('page2')}" update=":change />
    

    【讨论】:

    • 我设置为 SessionScoped,并添加了更新,结果和上面一样。
    • 为 actionListener="" 更改了 action="" ?
    • p:commandLink 中添加了 ajax ="false",但在 p:menuitem 中没有。
    • 我刚刚在 上尝试了 actionListener 和 ajax="false",但没有成功
    • 请看,action="" 应该与 ajax="false" 一起使用,并且最推荐用于发生页面导航时。并且 actionListener="" 应该和 ajax="true" 一起使用,并且推荐在没有页面导航的时候使用。所以在使用actionListener时,需要对需要更新的组件运行一个update=""。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多