从 Web 应用程序中的页面刷新中恢复?
是否有标准或可接受的方法可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以处理这个问题。
#inbox/123
时所做的操作,这意味着它应该显示带有标签收件箱的电子邮件 ID123
。 这不太具有表达性,但对于简单状态很有用。 但是,它确实为用户提供了为页面添加书签并使用浏览器历史记录进行导航的功能。There are a number of way to handle this.
#inbox/123
which means that it should show the email id123
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.我的建议是在服务器端为每个用户保留一个状态机,通过 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.
这是我们在项目中使用的一种解决方案:
您可以在用户访问页面时为页面分配一个序列号/随机 guid。 您可以将其放入 url 的 get 部分(例如 yourpage.jsp?pid=1337,其中 1337 是页面视图序列号)
当您处理 AJAX 请求时,您维护页面状态的“镜像”在会话中的服务器端或您可以在 JSP 中使用的任何机制来存储状态。
当用户请求页面时,您会检查给定的序列号是否已存在,如果存在,则意味着这是一次刷新,因此您可以使用会话中的镜像数据来呈现页面。
Here's one solution we used in a project:
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)
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.
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.