使用 URL 参数的 JSF Managed Bean 方法调用

发布于 2024-12-02 03:08:34 字数 309 浏览 1 评论 0原文

我有一个支持我的网络应用程序的会话范围托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2024-12-09 03:08:34

可以通过将作业侵入视图调用的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文