JSF 生命周期 - 请求完成后执行托管 bean 方法
通过 JSF 2.0 和 Spring,我使用 @RequestScope
托管 bean。该 bean 存储有关登录用户的信息。它通过@PostConstruct 方法从数据库加载用户:
@PostConstruct
public void init() {
String username = login.getUsername();
user = userDao.load(username);
}
然后,登录用户可以在页面上触发更新数据库中用户的操作(使用另一个托管 bean)。
但是,@RequestScope
bean 是在请求开始时构造的,即在调用更新操作之前。因此,当页面重新显示时,User
变量仍然具有其旧值。
我的问题是:我是否有办法不在请求开始时运行加载方法,而是在请求发送后运行?或者我以错误的方式处理这个问题?
感谢您的见解,
塞巴斯蒂安
Using JSF 2.0 and Spring, I use an @RequestScope
managed bean. This bean stores information about the logged-in user. It loads the user from the DB in a @PostConstruct
method:
@PostConstruct
public void init() {
String username = login.getUsername();
user = userDao.load(username);
}
The logged-in user can then trigger on action on the page that updates the user in database (using another managed bean).
However, the @RequestScope
bean is constructed at the beginning of the request, which is before the call to the updating action. As a result, when the page is redisplayed, the User
variable still has its old values.
My question is: do I have a way to run my loading method not at the beginning of the request, but after the request has been sent? Or am I dealing with this in the wrong way?
Thanks for your insight,
Sébastien
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相同托管 bean 应该已经在此时更新。如果由于某种原因无法重用相同的托管 bean,那么您应该通过在操作方法中访问它并自己调用 setter 来手动执行此操作。
更新:根据评论,以下是应如何在您的特定需求中声明、注入和使用bean:
The same managed bean should have been updated at that point. If you can't reuse the same managed bean for some reason, then you should manually do it by accessing it in the action method and calling the setters yourself.
Update: based on the comments, here's how the beans should be declared and injected and used in your particular requirement: