JSF 生命周期 - 请求完成后执行托管 bean 方法

发布于 2024-10-14 16:17:19 字数 511 浏览 3 评论 0原文

通过 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 技术交流群。

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

发布评论

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

评论(1

凡间太子 2024-10-21 16:17:19

然后,登录的用户可以在页面上触发更新数据库中用户的操作(使用另一个托管 bean)。

相同托管 bean 应该已经在此时更新。如果由于某种原因无法重用相同的托管 bean,那么您应该通过在操作方法中访问它并自己调用 setter 来手动执行此操作。


更新:根据评论,以下是应如何在您的特定需求中声明、注入和使用bean:

@ManagedBean(name="#{login}")
@SessionScoped
public class LoginManager {

    private String username;

    // ...
}

@ManagedBean(name="#{user}")
@RequestScoped
public class UserManager {

    @ManagedProperty(value="#{login}")
    private LoginManager login;
    private User current;

    @PostConstruct
    public void init() {
        current = userDAO.find(login.getUsername());
    }

    // ...
}

@ManagedBean(name="#{profile}")
@RequestScoped
public class ProfileManager {

    @ManagedProperty(value="#{user}")
    private UserManager user;

    public void save() {
        userDAO.save(user.getCurrent());
    }

    // ...
}

<h:form>
    <h:inputText value="#{user.current.firstname}" />
    <h:inputText value="#{user.current.lastname}" />
    <h:inputText value="#{user.current.birthdate}" />
    ...
    <h:commandButton value="Save" action="#{profile.save}" />
</h:form>

The logged-in user can then trigger on action on the page that updates the user in database (using another managed 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:

@ManagedBean(name="#{login}")
@SessionScoped
public class LoginManager {

    private String username;

    // ...
}

@ManagedBean(name="#{user}")
@RequestScoped
public class UserManager {

    @ManagedProperty(value="#{login}")
    private LoginManager login;
    private User current;

    @PostConstruct
    public void init() {
        current = userDAO.find(login.getUsername());
    }

    // ...
}

@ManagedBean(name="#{profile}")
@RequestScoped
public class ProfileManager {

    @ManagedProperty(value="#{user}")
    private UserManager user;

    public void save() {
        userDAO.save(user.getCurrent());
    }

    // ...
}

<h:form>
    <h:inputText value="#{user.current.firstname}" />
    <h:inputText value="#{user.current.lastname}" />
    <h:inputText value="#{user.current.birthdate}" />
    ...
    <h:commandButton value="Save" action="#{profile.save}" />
</h:form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文