在 JSF Managed bean 中初始化 List [英] Initialization of List in a JSF Managed bean

查看:27
本文介绍了在 JSF Managed bean 中初始化 List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在 POJO 中初始化 List 的问题,因为它遵循下一个代码:

I' have a question about initialization of List in the POJO as it follows the next code:

public class Person {

 //other fields...
 private List<String> friends=new ArrayList<>();

     public List<String> getFriends() {
        return friends;
     }
     public void setFriends(List<String> friends) {
        this.friends = friends;
    }

}

或者像这样更好并在其他类中初始化(例如Bean(JSF))

OR is it better like this and have initalization in other class(like for example Bean(JSF))

public class Person {

 //other fields...
 private List<String> friends;

     public List<String> getFriends() {
        return friends;
     }
     public void setFriends(List<String> friends) {
        this.friends = friends;
    }

}

所以我的问题是哪种方法更好?

So my question is what approach is better?

推荐答案

如果是你说的托管 bean,你应该在一个用 @PostConstruct 注释的方法中做到这一点

If it's a managed bean as you say, you should do this in a method annotated with @PostConstruct

public class Person {
    private List<String> friends;
    @PostConstruct
    public void init(){
         friends = new ArrayList<String>();
    }

    //getter and setter...
}

  1. 在 JSF 上下文中通常不赞成在 getter 和 setter 中进行任何初始化的做法.参见 为什么 JSF 多次调用 getter

此外,根据 的 API@PostConstruct,契约指定了安全特性并保证如果在这样注释的方法中抛出异常,则不应将 bean 投入使用.普通构造函数没有这样的保证.

Also, per the API for @PostConstruct, the contract specifies safety features and guarantees that if an exception is thrown in a method annotated as such, the bean should not be put into service. There are no such guarantees on a plain constructor.

在托管 bean 中,注入会在构造后立即发生.这意味着您在构造函数中执行的任何操作都不能依赖于任何注入的资源(通过 @ManagedProperty).而在 @PostConstruct 方法中,您将可以访问在托管 bean 上声明的所有资源

In a managed bean, injection happens immediately after construction. This means that any operations you're carrying out in the constructor cannot depend on any injected resources (via @ManagedProperty). Whereas in a @PostConstruct method, you'll have access to all the resources declared on the managed bean

需要注意的是,对于任何 @ManagedBean只能有一个 @PostConstruct,所以所有重要的初始化都应该发生在那里.

It's important to note that there can be only one @PostConstruct for any @ManagedBean, so all important initializations should happen in there.

还值得注意的是,虽然 @PostConstruct 方法是初始化支持 bean 变量/List 的理想场所,但对于托管bean

It's also worthwhile to note that, while the @PostConstruct method is the ideal place to initialize a backing bean variable/List, there are implications regarding the scope of the managed bean

  1. @RequestScoped:在带有此注释的托管 bean 中,每次提交相关 JSF 视图时都会调用该方法.@RequestScoped bean 会随着每个请求被销毁并重新创建,这意味着根据您的设置,在 @PostConstruct 中初始化的列表可能会重置为空或每个请求期间的默认值.在某些情况下,列表中 JSF 请求的重新初始化可能会导致转换错误.

  1. @RequestScoped: In a managed bean with this annotation, the method will be called per submit of the JSF view concerned. A @RequestScoped bean is destroyed and recreated with every request, The implication of this is that depending on your setup, the list initialized in the @PostConstruct may be reset to empty or default values during each request. Under certain circumstances, conversion errors may occur as a result of the re-initialization of the list mid-JSF request.

@ViewScoped:在带有此注解的托管 bean 中,您可以保证 @PostConstruct 方法运行一次,当且仅如果您正在处理 @ViewScoped bean 的相同实例.如果视图范围的 bean 被销毁并重新创建,@PostConstruct 方法将再次运行.

@ViewScoped: In a managed bean with this annotation, you're guaranteed to have the @PostConstruct method run once, if and only if you're dealing with the same instance of the @ViewScoped bean. If the viewscoped bean is destroyed and recreated, the @PostConstruct method will run again.

@SessionScoped:一个带有这个注解的 bean 被创建一次并且一直保持活动状态直到用户的 HTTP 会话结束.在这种情况下,@PostConstruct 方法保证只运行一次,直到 bean 被销毁

@SessionScoped: A bean with this annotation is created once and stays alive until the user's HTTP session ends. In this scenario, the @PostConstruct method is guaranteed to run once and only once until the bean is destroyed

另见

这篇关于在 JSF Managed bean 中初始化 List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆