Tapestry 应用程序中的会话超时 AJAX 错误
我正在使用 Tapestry 结合 Spring Security 和 Prototype 之外的 jQuery 库构建一个 Web 应用程序。当用户在会话超时后单击链接时,他会自动重定向到登录页面。当然,这不适用于触发 AJAX 请求的链接。
我知道,这是任何类型的 Web 应用程序的常见问题(例如 http://www .openjs.com/articles/ajax/session_timeout.php)。 Tapestry 5 是否有最佳实践解决方案?
编辑 以下解决方案(感谢 Henning)对我有用:
Ajax.Responders.register( { onException: function() { window.location.reload(); } });
如果 AJAX 调用期间发生故障,则会触发页面重新加载,从而重定向到登录页面。它仍然需要一些调整(例如显示错误消息而不是重定向),但使用 Ajax.Responders 基本上似乎是一个很好的方法。
I'm building a webapp using Tapestry in combination with Spring Security and the jQuery-library besides Prototype. When a user clicks on a link after his session timed out, he is automatically redirected to the login page. This, of course, does not work for links, that trigger an AJAX-request.
I know, this is a common problem with any kind of web application (e.g. http://www.openjs.com/articles/ajax/session_timeout.php). Is there a best practice solution for Tapestry 5?
EDIT
The following solution (thanks to Henning) works for me:
Ajax.Responders.register( { onException: function() { window.location.reload(); } });
In case of a failure during an AJAX-call a page reload is triggered, which in result redirects to the login-page. It still needs some tuning (e.g. display an error message instead of redirect), but using Ajax.Responders
basically seems a good way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于使用 Prototype 的 AJAX,您可以添加一个全局侦听器,使用 AJAX.Responders< /a>; jQuery 有一个类似的构造,名为 Ajax Events,您可以使用它。
两个事件处理程序都应该在出现 403 错误时重定向到登录页面。您可以创建一个具有此功能的 mixin 并将其添加到布局组件中。
我还使用了一种机制,可以在应用程序仍然在浏览器窗口中打开时防止会话超时,只需执行 AJAX 调用并每隔几分钟接收一个空响应,从而保持会话打开。很蠢,但是工作正常。
For the AJAX that uses Prototype, you could add a global listener that reacts to AJAX failures using AJAX.Responders; jQuery has a similar construct called Ajax Events that you could use.
Both event handlers should just redirect to the login page on a 403 error. You could create a mixin with this functionality and add it to your layout component.
I have also used a mechanism that prevents session timeouts while the app is still open in a browser window by just doing an AJAX call and receiving an empty response every couple of minutes, thus keeping the session open. Stupid, but works okay.
贡献T5主调度程序
你可以在你的AppModule.java中
所以每个没有会话的ajax请求,页面将重新加载并重定向到你的索引页面
you can contribute the T5 master dispatcher
In your AppModule.java
So every ajax request without session, the page will reloads and it redirects to your index page
好吧,向服务器发出 Ajax 请求,它发送标头“HTTP_X_REQUESTED_WITH”,值为“XMLHttpRequest”。您可以在服务器端检查是否是具有上述标头的 ajax 请求以及登录和会话超时的条件,然后再在索引页面中继续操作。
如果您的条件匹配,则只需在函数中打印“window.top.location.href='登录页面'”即可。
在 PHP 中,我可以这样做,
您可以在框架中添加与它类似的条件。
Well, Ajax request is made to server it sends the header "HTTP_X_REQUESTED_WITH" with value "XMLHttpRequest". You can just check serverside that whether it is ajax request with above header and condition for login and session timeout before proceeding further in your index page.
If your criteria gets matched then simply print "window.top.location.href='login page'" in your function.
In PHP i can do this as ,
You can add the condition similar to it in your framework.