从 web 应用程序更新 url 栏以表示当前状态

发布于 2024-11-04 21:13:18 字数 709 浏览 1 评论 0原文

我基本上想做杰森要求的事情 这里

在一句话中,我希望 url 栏代表 AJAX 应用程序的状态,以便我可以为其添加书签,并允许用户使用后退/前进按钮返回到之前的状态在浏览器中。

对我来说,不同之处(据 Jason 询问)是我使用的是 JSF 2.0。 我读到 JSF 2.0 添加了使用 get 的功能,但我不确定使用它的正确方法是什么。

感谢您的帮助。

进一步说明

如果我理解正确,为了能够在 AJAX Web 应用程序中为特定状态添加书签,我将必须使用 location.hash。我说得对吗?我正在尝试实现类似 gmail 的行为,即虽然应用程序已完成 AJAX 化并且不会发生重定向,但我仍然可以使用后退/前进和书签(这就是为什么我希望 URL 栏从AJAX 应用程序本身而不是通过重定向)

更新

刚刚找到这个类似的问题

I would like to basically do what Jason asked for here

In one sentence, I would like the url bar to represent the state of the AJAX application so that I can allow to bookmark it as well as allow the user to return to the previous state by using the back/forward buttons in the browser.

The difference for me (From what Jason asked) is that I am using JSF 2.0.
I've read that JSF 2.0 added the ability to use get, but I am not sure what the correct way to use this.

Thanks for the help.

Further Clarification

If I understand correctly, to be able to bookmark specific states in the AJAX webapp I will have to use the location.hash. Am I correct? I'm trying to achieve a gmail-like behaviour in the sense that, while the app is complete AJAXified and no redirects occur, I can still use Back/Forward and bookmark (And that's why I would like the URL bar to be updated from the AJAX app itself and not through redirection)

Update

Just found this similar question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失去的东西太少 2024-11-11 21:13:18

对我来说,不同之处(根据 Jason 的要求)是我使用的是 JSF 2.0。我读到 JSF 2.0 添加了使用 get 的功能,但我不确定使用它的正确方法是什么。

请注意,这与维护 Ajax 状态不同。通常是通过片段标识符(URL 中以 # 开头的部分,也称为 hashbang)发生的。 JSF 不为此提供内置组件/功能。到目前为止我还没有看到一个组件库可以做到这一点。但是,您可能会发现 这个答案对于开始使用 JSF 中的本地哈希片段处理器很有用。

至于使用 GET 请求,只需使用 甚至 来创建获取链接。您可以通过 h: 组件中提供请求参数。例如,

<h:link value="Edit product" outcome="product/edit">
    <f:param name="id" value="#{product.id}" />
</h:link>

product/edit.xhtml 页面中,您可以定义要在 GET 请求上设置的参数和执行的操作

<f:metadata>
    <f:viewParam name="id" value="#{productEditor.id}" />
    <f:event type="preRenderView" listener="#{productEditor.init}" />
</f:metadata>

在与 product/edit.xhtml 关联的请求或视图作用域 bean 中> 页面 - 在此示例中 #{productEditor} - 您只需定义属性和侦听器方法。在模型中收集、转换、验证和更新所有属性后,将执行侦听器方法。

private Long id;
private Product product;

public void init() {
    product = productService.find(id);
}

The difference for me (From what Jason asked) is that I am using JSF 2.0. I've read that JSF 2.0 added the ability to use get, but I am not sure what the correct way to use this.

Please note that this is not the same as maintaining the Ajax state. It usually happens by fragment identifiers (the part starting with # in URL, also known as hashbang). JSF doesn't offer builtin components/functionality for this. As far I have also not seen a component library which does that. You may however find this answer useful to get started with a homegrown hash fragment processor in JSF.

As to using GET requests, just use <h:link>, <h:outputLink> or even <a> to create GET links. You can supply request parameters in the h: components by <f:param>. E.g.

<h:link value="Edit product" outcome="product/edit">
    <f:param name="id" value="#{product.id}" />
</h:link>

In the product/edit.xhtml page you can define parameters to set and actions to execute upon a GET request

<f:metadata>
    <f:viewParam name="id" value="#{productEditor.id}" />
    <f:event type="preRenderView" listener="#{productEditor.init}" />
</f:metadata>

In the request or view scoped bean associated with product/edit.xhtml page -in this example #{productEditor}-, you just define the properties and the listener method. The listener method will be executed after all properties are been gathered, converted, validated and updated in the model.

private Long id;
private Product product;

public void init() {
    product = productService.find(id);
}
淡水深流 2024-11-11 21:13:18

通常您会使用 AJAX 来阻止完整的页面刷新。 AFAIK 如果您更改基本 uri,所有当前浏览器都会发出页面刷新问题。因此,您必须按照您提供的问题中的建议使用哈希部分。

我们遇到了类似的问题并做了类似的事情:

  1. 我们解决了用户无法为网址添加书签的事实。
  2. 对于应该是唯一的/可添加书签的 URL,我们使用了发出重定向的不同链接。这些 URL 在站点地图中提供。
  3. 对于浏览器返回,我们在登录后添加了一个中间页面。该页面进行导航并重定向到应用程序。导航存储在会话中,当服务器收到导航请求(可以是历史记录)时,将恢复相应的状态。浏览器返回打开该中间页面,该页面在服务器端发出重定向和导航请求。

Normally you'd use AJAX to prevent complete page refreshes. AFAIK all current browsers would issue a page refresh if you change the base uri. Thus you would have to use the hash part as suggested in the question you provided.

We had a similar problem and did something like this:

  1. We settled for the fact that users cannot bookmark the url.
  2. For URLs that should be unique/bookmarkable we used different links that issue a redirect. Those URLs are provided in a sitemap.
  3. For browser back, we added an intermediate page after login. This page does navigation and a redirect to the application. The navigation is stored in the session and when the server gets a navigation request (which can be a history back) the corresponding state is restored. A browser back opens that intermediate page which issues a redirect along with a navigation request on the server side.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文