使用 URL 参数的 JSF Managed Bean 方法调用
我有一个支持我的网络应用程序的会话范围托管 bean。在此网络应用程序中,我希望用户能够执行包含用户 ID 和日期的 URL。然后,这将触发托管 bean 中的一个方法,并将 Web 应用程序跳转到该用户 ID 和日期。
简单的答案是将用于从 URL 提取参数的代码放在托管 bean 的构造函数中。但问题在于它是会话范围的,因此构造函数仅在初始加载时被调用。如果用户打开 Web 应用程序并对其进行操作,然后使用新参数执行 URL,则无法调用构造函数。
有什么方法可以让我在每次执行应用程序 URL 时(每次加载页面时)在托管 Bean 上执行方法,而不管会话状态如何?
I have a Session Scoped managed bean that backs my web-app. In this web-app I would like to have the ability for the user to execute a URL containing a user id and a date. This will then trigger a method in the managed bean, and jump the web-app to that user id and date.
The easy answer would be to put the code for extracting parameters from the URL in the constructor of the managed bean. But the problem lies in the fact that it is Session Scoped, so the constructor is only called on initial load. If the user opens the web-app and manipulates it, then executes the URL with new parameters, there is no way for the constructor to be called.
Is there some way that I can execute a method on a Managed Bean every time the applications URL is executed (every time the page is loaded) regardless of Session state?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以通过将作业侵入视图调用的 getter 来实现,但不,你真的不想拥有它。会话作用域 bean 在同一会话中的所有浏览器窗口/选项卡之间共享。窗口/选项卡中的每次更改都会影响所有其他窗口/选项卡。这可能会导致“wtf?”因此,这对网站的一般 UX(用户体验)来说是不利的。
如果您想拦截 GET 请求,或者如果您使用 JSF 2.0,如果您想在初始 GET 请求之后的后续 POST 请求中维护状态,那么您确实需要将该 bean 放入请求范围内。会话范围 bean 旨在保存会话范围数据,例如登录用户、其首选项等。它不适用于请求范围内的数据,例如请求参数等。
It's possible by hacking the job into a getter which is called by the view, but no, you really don't want to have it. A session scoped bean is shared between all browser windows/tabs within the same session. Every change in a window/tab will affect all other windows/tabs. This may lead to "wtf?" experiences which is thus bad for general UX (User Experience) of your site.
You really need to put that bean in the request scope if you want to intercept on GET requests, or if you're using JSF 2.0, the view scope if you want to maintain the state in subsequent POST requests after the initial GET request. A session scoped bean is intented to hold session scoped data, such as the logged-in user, its preferences, etcetera. It is not intended for request scoped data such as request parameters and like.