JQuery Address 管理应用程序状态的简单示例
我正在使用 jQuery Address 库根据用户在页面中执行的操作来重写我的 URL。目的是允许用户为页面添加书签并稍后返回。该页面永远不会刷新,因为所有服务器交互都是通过 ajax 完成的。
jQuery Address 的 URL 写法如下:
http://localhost:9000/#/u/scott_tiger
我需要在 Play 中设置一条路由,以便能够将该请求路由到适当的控制器。所以我设置了这个:
GET /#/u/{username} Controller.showUser
但这不起作用,路由定义被忽略。我尝试了很多事情,例如尝试转义“#”并将其替换为我用Character.toString(35)填充的变量。这些都不起作用。
有谁知道我如何正确定义路由或获取 jQuery 地址而不写“#”。
编辑:“#”不会发送到服务器。哎哟!好的,问题已修改。
I'm using the jQuery Address library to re-write my URL depending on what the user is doing in the page. The intention is to allow users to bookmark pages and come back later. The page never refreshes as all server interaction is done via ajax.
jQuery Address is writing URLs like this:
http://localhost:9000/#/u/scott_tiger
I need a to set up a route in Play to be able to route that request through to the appropriate controller. So I set this up:
GET /#/u/{username} Controller.showUser
This doesn't work though, the route definition gets ignored. I've tried loads of things such as trying to escape the "#" and replacing it with a variable that I've populated with Character.toString(35). None of this works.
Does anyone know how I can either define the route properly or get jQuery Address not to write the "#".
EDIT: The "#" doesn't get sent to the server does it. Doh! OK, question is revised.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不会。
#
及其后的 URL 部分不会发送到服务器。因此,您在服务器上的 Play 应用程序永远不会看到此类 URL。HTML5 解决方案
您需要使用 JavaScript 在客户端处理这些 URL。在具有良好 HTML5 支持的现代浏览器中,您可以修改地址而无需重新加载页面。请参阅操作浏览器历史记录了解如何针对这些浏览器执行此操作。请参阅何时可以使用...以获得浏览器支持。
#-URL
在 Internet Explorer 和其他浏览器的旧版本上,您需要使用
#
URL 并使用 JavaScript 来加载状态(例如,获取用户页面/u/scott_tiger
你的例子)。请参阅如何操作当用户使用 JQuery 访问哈希链接 (#something) 时运行 JavaScript 函数?了解如何在 JavaScript 中执行此操作。此外,如果用户使用#
-URL 将页面添加为书签,则您需要重新加载状态。另请参阅:什么是 shebang/hashbang (# !) 在 Facebook 和新的 Twitter URL 中?
JavaScript 库
您可以使用 JavaScript 库来处理这个问题 history.js 就是一个例子。像 Backbone.js 这样的更大的框架也可以处理这个问题。
如果不写 URL 的
#
部分,则无法链接状态。因此,如果您为该页面添加书签,则无法返回到例如 Scott Tigers 个人资料页面,因为 URL 只是http://localhost:9000/
并且您将到达首页,而用户尽管他会到达个人资料页面。No. The
#
and the part of the URL after that is not sent to the server. So your play app on the server will never see such URLs.HTML5 solution
You need to handle these URLs on the client side using JavaScript. In modern browsers with good HTML5 support, you can modify the address without reloading the page. See Manipulating the browser history on how to do it for these browsers. And see When can I use... for browser support.
#-URLs
On Internet Explorer and older versions of other browsers you need to use
#
URLs and use JavaScript to load the state (e.g. get the user page/u/scott_tiger
in your example). See How to run a JavaScript function when the user is visiting an hash link (#something) using JQuery? for how to do this in JavaScript. Also if a user bookmarks a page with a#
-URL you need to reload the state.See also: What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for?
JavaScript libraries
You may use JavaScript libraries to handle this for you history.js is an example. Also bigger frameworks like Backbone.js handle this.
If you don't write the
#
-part of the URL, the state can not be linked. So you can not get back to e.g. Scott Tigers profile page if you bookmark the page, because the URL is onlyhttp://localhost:9000/
and you will arrive on the front page, while the user though he would arrive on the profile page.有了对 URL 的新理解(感谢@Jonas),我意识到我错过了一半的故事。
我使用 JQuery 地址 根据您在应用程序中单击的内容更改 URL。这在很多浏览器上都很有效。我缺少的是使用 JQuery Address 来监视外部地址更改(书签、历史记录、返回/转发)并做出相应的回应。即通过触发适当的 Ajax 调用并适当地呈现该数据来正确设置页面。
更改地址
恢复状态
Armed with my new understanding of URLs (thanks @Jonas) I realised that I'd missed half of the story.
I'm using JQuery Address to change the URL depending on what you click in the application. This works great and on lots of browsers. What I was missing was using JQuery Address to watch for external address changes (bookmarks, history, back/forward) and respond accordingly. i.e. set the page up correctly by firing the appropriate Ajax calls and rendering that data appropriately.
Changing the address
Restoring the state