简体   繁体   English

使用JSF ajax请求刷新URL

[英]Refreshing URL with JSF ajax request

I'm using JSF 2 with PrettyFaces and also the <h:link> and <f:param> tags extensively to get bookmarkable pages (It's great to finally forget about all those POST request in a JSF application). 我正在使用JSF 2和PrettyFaces以及<h:link><f:param>标签来获取可收藏的页面(最终忘记JSF应用程序中的所有POST请求真是太棒了)。

However, some of these links only changes a small part of the page (mainly when the user stays in the same view), so it's nicer to send ajax requests instead. 但是,其中一些链接只会更改页面的一小部分(主要是当用户停留在同一视图中时),因此发送ajax请求会更好。 However, <f:ajax> cannot be used with <h:link> , just <h:commandlink> . 但是, <f:ajax>不能与<h:link> ,只能<h:commandlink> I can send parameters many ways with a commandlink as well, but in this case the URL stays the same after the request. 我也可以使用commandlink以多种方式发送参数,但在这种情况下,URL在请求后保持不变。 So it becomes out of sync with the content of the page, and it's not refreshable. 因此它与页面内容不同步,并且不可刷新。

I've seen sites that use links that send kind of a mixture of GET and ajax requests (pointing to a new url while modifying the page with ajax), but so far I haven't found any solution. 我已经看到使用链接发送GET和ajax请求(在使用ajax修改页面时指向新URL)的网站,但到目前为止我还没有找到任何解决方案。

Can I somehow refresh the url with jsf after the ajax request finishes (or when the user clicks)? 在ajax请求完成后(或用户点击时),我可以用jsf以某种方式刷新URL吗? Or do I have to use some external JS library? 或者我是否必须使用一些外部JS库?

EDIT : The JS library that is needed here is the new JavaScript History API. 编辑 :这里需要的JS库是新的JavaScript History API。 More info. 更多信息。 I guess JSF does not use this API currently. 我猜JSF目前不使用这个API。 But I'm still interested in this issue from a JSF developer's point of view. 但是我仍然从JSF开发人员的角度对这个问题感兴趣。

Also, is it planned for a future release of JSF? 此外,它是否计划在未来发布JSF? Combining <f:ajax> with <h:link> would be a perfect solution. <f:ajax><h:link>将是一个完美的解决方案。

<f:ajax> has attribute onevent . <f:ajax>具有属性onevent It takes name of javascript function which will be called after ajax request successfully finishes. 它采用javascript函数的名称,它将在ajax request成功完成后调用。 So it is possible to add js function which will navigate to required url after ajax call finishes. 因此可以添加js函数,该函数将在ajax调用完成后导航到所需的url。 Something like: 就像是:

        <h:commandLink id="testButton"
                       value="Test">
            <f:ajax event="action"
                    execute="@form"
                    render="@none"
                    listener="#{myBean.testButtonClickedHandler}"
                    onevent="navigate"/>
        </h:commandLink>

<script type="text/javascript">
    function navigate() {
        window.location.href = "required url";
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM