selectOneMenu 中的空 bean

null bean in selectOneMenu

所以我的用户个人资料页面中有一个 selectOneMenu。它的目标值是我的用户配置文件 table 中的一个字段。该字段是另一个 table 的外键。最初这个值为空,因为用户还没有设置他的信息。

select 一个菜单如下所示:

<p:selectOneMenu value="#{userProfileEdit.uinfo.datBean}" >  <!-- This Value is null initially -->
    <f:selectItem itemLabel="Select One" itemValue="#{null}" />
    <f:selectItems value="#{datBeanConverter.beansList}" var="bean" itemLabel="#{bean.title}" itemValue="#{bean}"/>
</p:selectOneMenu>

该值最初为空,我收到此错误:

value="#{userProfileEdit.uinfo.datBean}": Target Unreachable, 'null' returned null

如果可以的话,我该如何解决这个问题?

编辑:我的 uinfo 属性 bean 像这样在 userProfileEdit 中初始化

@ManagedBean
@RequestScoped
public class UserProfileEdit implements Serializable {
    private Userinfo uinfo;
    @EJB
    private UserProfileService us;
    //...

    @PostConstruct
    public void init() {
        setUser();
        this.uinfo = us.getUserInfo(username);
    }
    //...
}

可以在bean的postconstruct方法中初始化。

@PostConstruct
public void init() {
...
    UserInfo uinfo= new UserInfo();
    //you will need to initialize the property datBean of UserInfo in the Constructor.
...
}

附加信息:

@PostConstruct

希望对您有所帮助。

像这样定义新的 datBean 变量并将其放入值属性中

<p:selectOneMenu value="#{userProfileEdit.datBean}" >  <!-- This Value is null initially -->
       <f:selectItem itemLabel="Select One" itemValue="#{null}" />
       <f:selectItems value="#{datBeanConverter.beansList}" var="bean" itemLabel="#{bean.title}" itemValue="#{bean}"/>
   </p:selectOneMenu>

然后在提交表单后,您可以创建 uinfo 对象并使用 datBean 对象。