从 Web 应用程序中的页面刷新中恢复?

发布于 2024-07-21 23:44:57 字数 224 浏览 10 评论 0原文

是否有标准或可接受的方法可以从 Ajax Web 应用程序中的页面刷新中恢复?

我现在的兴趣主要在于具有 Java(JSP/Servlet)后端的 Web 应用程序。 我的页面最初是从 JSP 呈现的,然后用户使用 javascript 事件在界面中前进。

是否有一种设计模式涵盖了这一点,我假设刷新按钮是 Web 开发人员需要经常担心的事情,因此应该有一种方法可以从中恢复,同时保持状态。

Is there a standard or accepted way to recover from a page refresh in an Ajax web application?

My interest now lies mainly in a web app with a Java (JSP/Servlet) Back end. My page is initially rendered from a JSP and then the user progressed through the interface using javascript events.

Is there a design pattern which covers this, I'm assuming that the refresh button is someting that web developers need to worry about quite often so there should be a way of recovering from it, while maintaining state.

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

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

发布评论

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

评论(3

浴红衣 2024-07-28 23:44:57

有多种方法可以处理这个问题。

  • 锚点 - 这是 Gmail 在附加 #inbox/123 时所做的操作,这意味着它应该显示带有标签收件箱的电子邮件 ID 123 。 这不太具有表达性,但对于简单状态很有用。 但是,它确实为用户提供了为页面添加书签并使用浏览器历史记录进行导航的功能。
  • Cookie - 这样做的优点是可以完全在客户端进行管理。 您可以通过 Javascript 设置 cookie 并通过 Javascript 恢复它们。 它很便宜,而且不需要回邮。 状态信息通常不需要保留在服务器上,因为状态通常是临时的。
  • 会话 - 当客户端更新页面时,这将需要您通过 AJAX 将状态信息发回服务器。 如果客户端刷新页面,新页面会将更改后的状态合并到新呈现的页面中。 这在性能方面成本相当高,而且设计也变得复杂,但对于某些应用可能很有用。

There are a number of way to handle this.

  • Anchors - This is what Gmail does when it tacks on #inbox/123 which means that it should show the email id 123 with the label inbox. This is not very expressive and is useful for simple states. However, it does provide users the ability to bookmark the page and use navigate through browser history.
  • Cookies - This has the advantage that this can be managed entirely on the client side. You can set cookies via Javascript and restore them via Javascript. It's cheap and doesn't require and post backs. The state information usually doesn't need to be persisted on the server because typically the state is temporary.
  • Sessions - This will need you to post back the state information back to the server via AJAX as the client updates the page. If the client refreshes the page, the new page incorporates the changed state into the newly rendered page. This is quite costly in terms of performance and also complicates design but may be useful for certain applications.
时光清浅 2024-07-28 23:44:57

我的建议是在服务器端为每个用户保留一个状态机,通过 AJAX 调用更改状态。 这样,在刷新时,您就已经知道用户处于哪个状态位置,从而允许您从中恢复。

如果您在编码时不小心,这可能会给您带来可扩展性问题。

另一种解决方案可能是将状态变量存储在 cookie 中(假设用户具有活动 cookie)。 在页面加载时,状态变量将被提交到您的 Web 应用程序,以便您进行恢复。

My suggestion would be keeping a state machine for each user on server side which changes states with the AJAX calls. That way on the refresh, you'll already know in which state position the user was in, allowing you to recover from this.

This might bring you a problem with scalability if you are not careful while coding it.

Another solution might be storing the state variable in the cookie (assuming the user has active cookies). And on page load, the state variable would be submitted to your web application, allowing you to recover.

寻找我们的幸福 2024-07-28 23:44:57

这是我们在项目中使用的一种解决方案:

  1. 您可以在用户访问页面时为页面分配一个序列号/随机 guid。 您可以将其放入 url 的 get 部分(例如 yourpage.jsp?pid=1337,其中 1337 是页面视图序列号)

  2. 当您处理 AJAX 请求时,您维护页面状态的“镜像”在会话中的服务器端或您可以在 JSP 中使用的任何机制来存储状态。

  3. 当用户请求页面时,您会检查给定的序列号是否已存在,如果存在,则意味着这是一次刷新,因此您可以使用会话中的镜像数据来呈现页面。

Here's one solution we used in a project:

  1. You assign a sequence number / random guid to the page eash time the user visits the page. You can put that into the get part of the url (such as yourpage.jsp?pid=1337 where 1337 is the page view sequence number)

  2. When you process the AJAX requests, you maintain a "mirror" of the page state on the server side in the session or whatever mechanism you can use in JSP to store state.

  3. When the user requests the page, you check if the given sequence number already exists, and if it does, it means that it's a refresh so you render the page using the mirror data you have in your session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文