Struts2后退按钮和链接
我正在使用 Struts 2.1.6 和 Dojo 插件,整个应用程序都有 ajax 链接 (sx:a)。
有人成功实现后退按钮功能并链接到某些内容吗?
有人有如何实施的经验吗?我计划实现(如果已经没有好的解决方案)这样的事情:
- 用js更改地址栏链接(添加参数),然后我可以读取并获取正确的内容,然后使用notifyTopics发布它。
或者我应该改变整个应用程序以使用 jQuery 插件? jQuery 对于后退按钮和 ajax 页面上的链接有好的解决方案吗?
I am using Struts 2.1.6 with Dojo plugin, whole app has ajax links (sx:a).
Did anybody succeed to implement back button functionality and linking to certain content?
Does anybody have any experience how to implement? I am planning to implement (if there is no good solution already) something like so:
- changing address bar link (adding parameters) with js which I can then read and get proper content and then publish it with notifyTopics.
Or should I just change whole app to use jQuery plugin? Do jQuery has good solutions for back button and linking on ajax pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以立即想到两种简单的方法:
或者
如果您已经有查询字符串参数:
或者
I can think of 2 simple ways off the top of my head:
or
If you already have query string parameters:
or
我尝试将 Struts 2 与 dojo 一起使用并实现后退按钮。您已经完全掌握了 Struts 2 的 ajax 实现。他们使用和编写它主要是为了编写简单快速的ajax函数调用,不太适合更广泛的用途。另外,当您执行 s:head theme='ajax' tag; 时struts 将导入您“可能”需要的每个 ajax js 文件,这会缩短加载时间。
我建议要么1.学习dojo并使用独立于struts 2的库。要么2.获取jQuery,我能够实现相对简单的后退按钮功能(比struts 2 theme='ajax'更是如此)。
I have tried to use Struts 2 with dojo and implement the back button. You are already way over the head of Struts 2's ajax implementation. They mainly used and wrote it to write simple and quick ajax function calls and is not very well suited for more extensive uses. Plus when you do s:head theme='ajax' tag; struts will import every ajax js file you 'may' need which kills load time.
I would suggest either 1. Learn dojo and use the library independent of struts 2. Or 2. Get jQuery, I was able to implement a back button functionality relatively simple (more so then struts 2 theme='ajax').
不了解 Struts,但是您看过 dojo.undo (0.4.3) 吗?
Don't know Struts, but have you looked at dojo.undo (0.4.3)?
我的所有链接都经过一个操作,该操作查找参数menuId(当然是必须显示的菜单的id)。
从这个动作中,在返回响应之前,我设置了一个要调用的 JavaScript 函数:
MenuId 是 id,sometext 是该菜单的名称,以便浏览器记录历史记录更好。
然后,其他需要的js函数:
如果你看一下你会发现,首先它被称为“setBackMenuId”,它将哈希和参数添加到地址栏并更改标题,然后记住这个哈希,因此区间哈希搜索可以找到差异。然后它初始化这个哈希搜索。
“checkHash”每隔 500 毫秒运行一次,并检查哈希值是否已更改(这意味着按下了后退按钮,并且没有单击新链接(setBackMenuId 设置 selectedHash)。如果为 true(按下后退/前进按钮),则函数 '调用publishLinkTarget',它从哈希中读取参数,如果它们没问题,首先我设置一个cookie,这样我就可以从HttpServletRequest中读取它并找出哪个菜单id链接。如果我在这里,这意味着我。还必须发布使用 'publishLayoutContent' 制作的内容
(这是 MenuAction,方法视图,与在 中发布的相同),只有这一点很重要:
所以,如果我没有从参数中获取菜单 id。 (单击链接)我从 cookie(后退/前进按钮)获取
此目标的 JSP:
注意:如果您通过一个参数连接所有内容,则这是一种特殊情况,但它可以轻松地使用发布其他参数的其他参数进行扩展。我会尝试使其足够通用以将其发布到某个地方,但(我猜)这还有很长的路要走:)
如果您有任何问题,请发布。
All my links go through one action which looks for a parameter menuId (of course id of a menu which has to be shown).
From this action, before returning response I set one javascript function to be called:
MenuId is the id, sometext is a name of that menu, so browser log history better.
Then, other needed js functions:
If you look at it you see, that first it is called 'setBackMenuId', which adds hash and parameter to address bar and changes title, and then remembers this hash, so interval hash search can find out the differrence. Then it initializes this hash search.
'checkHash' is running ever 500 miliseconds and is checking if hash has changed (that means, that back button was pressed, and not a new link was clicked (setBackMenuId sets selectedHash). If true (back/forward button was pressed) function 'publishLinkTarget' is called, which reads the parameters from hash, and if they are ok, first I set a cookie, so I can read it from the HttpServletRequest and find out for which menu id link is. If I am here it means that I have to also publish the content which is made with 'publishLayoutContent'.
In action class (this is MenuAction, method view, the same as published in ) only this is important:
So, if I don't get the menu id from the parameter (link clicked) I get from a cookie (back/forward button).
And JSP with this target:
NOTE: This is a special case if you have everything connected through one parameter, but it can be easily extended with other parameters which publish other targets. I will try to make it enough generic to publish it somewhere, but this is (I guess) a long way ahead :)
If you have any question, please post it.