null bean in selectOneMenu



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

选择一个菜单如下所示:

<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);
    }
    //...
}

定义新的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对象。

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

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

其他信息:

@构造后

目标无法访问

希望这能有所帮助。

最新更新