跨多个请求周期保留组件状态
关于使用 Apache Wicket 1.4.x 进行组件状态管理的问题
我有一个可添加书签的有状态页面,其中包含带有一组搜索条件的表单。用户搜索与所选条件匹配的项目,然后导航离开页面(例如,转到详细的项目视图)。在某个时间点,用户可以使用可添加书签的链接返回到搜索页面。我希望将最后一个搜索条件预先选择为默认选择。但是,BookmarkablePageRequestTarget
似乎在其 #respond(RequestCycle)
方法中创建了目标页面的新实例,从而丢失了先前的状态(支持搜索表单的 bean) 。
当然,我可以手动管理表单状态并将其存储在 WebSession
子类中,但我想知道是否有更好的方法来跨多个请求周期保留组件状态,例如使 Wicket 重新- 使用目标页面的现有实例?理想情况下,这适用于书签链接。
Question about component state management with Apache Wicket 1.4.x
I have a bookmarkable, stateful page that contains a form with a set search criteria. The user search for items matching the selected criteria and then navigate away from the page (to a detailed item view, for instance). At some point of time the user can come back to the search page using a bookmarkable link. I would like the last search criteria to be pre-selected as a default choice. However, it appears that BookmarkablePageRequestTarget
aways creates a new instance of the target page in its #respond(RequestCycle)
method, thus losing previous state (the bean backing the search form).
Naturally, I could manually manage the form state and store it in the WebSession
subclass, but I am wondering whether or not there might be a better way to preserve component state across multiple request cycles, like making Wicket re-use the existing instance of the target Page? Ideally this would work with bookmarkble links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,组件的状态存储在组件实例本身中。 (直接在其中,或间接在模型中。)
因此,您真正需要做的就是创建一个指向搜索页面实例的链接,或调用
setResponsePage() 与页面实例或类似的内容,具体取决于您重定向用户的方式。
通常,您根本不应该触及请求目标,除非实际情况比您所描述的情况复杂得多。
Normally, the state of a component is stored in the component instance itself. (Either directly in it, or indirectly, in models.)
So all you really need to do is to create a link that points to the search page instance, or call
setResponsePage()
with the page instance or something similar, depending on how you're redirecting the user.Normally, you shouldn't even touch request targets at all, unless the actual scenario is much more complex than the one you're describing.
使用 HybridUrlCodingStrategy 或其子类。这会将页面标识符附加到 URL 并确保后退按钮适用于可添加书签的链接。
Mount your bookmarkable page with HybridUrlCodingStrategy or its subclass. This will append a page identifier to the URL and ensure that the back button works with bookmarkable links.