代码之家  ›  专栏  ›  技术社区  ›  user1156544

方法调用后更新URL参数

  •  0
  • user1156544  · 技术社区  · 7 年前

    这应该很简单,但我正在考虑其他问题,我无法找到我的问题的正确答案。

    我有一个jsf页面,它调用一个方法 myController.load() :

    <f:metadata>
         <f:viewParam name="id" value="#{myController.id}" required="false"/>
         <f:viewAction action="#{myController.load()}" />
    

    此方法将生成 id 放在 myController.id 如果最初没有使用这样的参数调用页面。我的问题是如何使导航栏上的url反映这一更改,即在url中插入这个新参数。基本上:

    -->浏览到 myPage.xhtml

    -->呼叫 mycontroller.load() 哪一套 mycontroller.id号 =1个

    -->在URL中反映 myPage.xhtml?id=1 . 理想情况下不需要重新加载页面

    1 回复  |  直到 7 年前
        1
  •  1
  •   Clint Munden    7 年前

    你会想做一些关于prg的阅读(post/redirect/get)。这是一个好的开始。

    https://mobiarch.wordpress.com/2012/08/09/doing-post-redirect-get-pattern-in-jsf-2/

    您希望原始的jsf链接调用一个actionlistener,该actionlistener将创建对mycontroller的作用域引用,并在那里将id属性设置为1……然后您可以使用PRG重定向到您的页面PRG将根据需要使用bean的值正确地构建新的URL。

    这个actionListener方法非常简单,只是为了帮助说明…

    // my ActionListener method
    public String goToMyPage() {
        myController.setId(1); // assuming myController is declared in scope.
    
        return "myPage?faces-redirect=true&includeViewParams=true";
    }
    

    希望这能帮你开始。