在AJAX导航更改网址网址、AJAX

2023-09-10 20:47:51 作者:我的未来我做主

我有一个JSF应用程序,我想在浏览器地址栏中的网址导航上的改变。当我在 Home.xhtml 和我提交表单,它显示下一个页面 AppHome.xhtml ,但不改变在浏览器地址栏中的网址。

I have a JSF application and I want the URL in browser address bar be changed on navigation. When I'm on Home.xhtml and I submit a form, it shows the next page AppHome.xhtml, but the URL in browser address bar is not changed.

下面的提交按钮:

<p:commandButton value="Connect" update="panel" id="ajax" action="#{user.check}" styleClass="ui-priority-primary"/>

下面的导航规则:

<navigation-rule>
    <from-view-id>/Home.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>Success</from-outcome>
        <to-view-id>/AppHome.xhtml</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>Failure</from-outcome>
        <to-view-id>/Home.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

我要如何更改浏览器地址栏中的网址时,执行该导航?

How do I change the URL in browser address bar when this navigation is performed?

推荐答案

在浏览器地址栏中的URL,当一个同步请求被解雇在一个不同的URL不是当前请求的URL仅改变。一个Ajax请求是不同步的请求。因此,在浏览器地址栏中的URL不会改变。此外,JSF在默认情况下提交表单当前的URL(检查动作的属性生成的HTML &LT;形式GT; ),所以URL也会永远不变的同步POST请求。

The URL in browser address bar is only changed when a synchronous request is been fired on a different URL than the current request URL. An ajax request is not a synchronous request. So the URL in browser address bar will never change. Also, JSF submits the forms by default to the current URL (check the action attribute of the generated HTML <form>), so the URL will also never change on synchronous POST requests.

您需要,为了改变在浏览器地址栏中的URL不同的URL发送一个同步GET请求。一个重定向将迫使同步GET请求。重定向/&GT; 进入该&LT;导航的情况下&gt;您可以通过添加&LT做到这一点。

You need to send a synchronous GET request on a different URL in order to change the URL in browser address bar. A redirect will force a synchronous GET request. You can achieve this by adding <redirect/> entry to the <navigation-case>.

您对执行重定向厌恶完全没有理由。这是最有可能在其他地方造成的,因此需要在其他地方解决。它是缓慢的页面加载和/或内容(闪烁)的闪光?这个问题有反过来无关重定向和需要解决不同。这是为了通过缓存适当地解决。或者是,你在重定向丢失的面孔消息?客人可以使用闪光范围通过Flash#setKeepMessages().

Your aversion against performing a redirect has absolutely no grounds. It's most likely caused elsewhere and thus needs to be solved elsewhere. Is it the slow page loading and/or flash of content (flickering)? This problem has in turn nothing to do with redirects and needs to be solved differently. This is to be solved by caching appropriately. Or is it that you're losing faces messages on redirect? Make use of the flash scope via Flash#setKeepMessages().

How使URL反映当前页面(而不是previous一个) JSF负载时页面闪烁,而在PROJECT_STAGE发展 JSF缓存的静态资源过滤器 How要显示重定向的页​​面 faces消息 How to make URL reflect the current page (and not the previous one) JSF page flashes during load while in project_stage development JSF Cache Static Resources Filter How to show faces message in the redirected page

无关的具体问题,导航案件洙JSF 1.x的你是否知道新的JSF 2.x的隐式导航功能的?请确保您正在阅读最多针对JSF 2.x的,而不是针对JSF 1.x的那些日子JSF教程。

Unrelated to the concrete problem, navigation cases are soo JSF 1.x. Are you aware of the new JSF 2.x implicit navigation feature? Make sure that you're reading up to date JSF tutorials targeted at JSF 2.x instead of the ones targeted at JSF 1.x.

您可以摆脱整体&LT;导航规则&GT; 完全如果您实现操作方法如下:

You can get rid of the whole <navigation-rule> altogether if you implement the action method as follows:

public String check() {
    // ...

    if (fail) {
        return null; // Goes back to same page while keeping JSF view state.
    } else {
        return "/AppHome?faces-redirect=true";
    }
}