selectOneMenu中的null bean

时间:2015-06-22 21:30:45

标签: jsf managed-bean

所以我的用户个人资料页面中有一个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>

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

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

2 个答案:

答案 0 :(得分:2)

定义新的datBean变量并将其放在value属性中,如此

<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对象。

答案 1 :(得分:1)

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

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

其他信息:

@PostConstruct

Target Unreachable

希望这有帮助。

相关问题