当使用来自不同辅助bean的selectItems时,selectOneMenu值为null

时间:2018-06-06 01:51:31

标签: java jsf

我有一个用于创建用户的表单。在我的表单中,我有该用户的多个属性(我实际上使用一个User对象来保留提交给支持bean的数据)

create.xhtml

            <h:form>
                <h:outputLabel for="user_name" value="Name:" />
                <h:inputText id="user_name" value="#{createUserView.newUser.username}" />

                <br/><br/>

                <h:outputLabel for="user_password" value="Default Password*:" />
                <h:inputSecret id="user_password" value="#{createUserView.newUser.password}"></h:inputSecret><br/><br/>

                <h:outputLabel for="user_organization" value="Organization:" />
                <h:selectOneMenu id="user_organization" disabled="true" value="#{createUserView.newUser.organizationId}">
                    <f:selectItems 
                        value="#{organizationBean.allOrganizations}" 
                        var="org"
                        itemLabel="#{org.organizationName}"
                        itemValue="#{org.id}" />
                </h:selectOneMenu><br/><br/>

                <h:commandButton value="Create" action="#{createUserView.createNewUser}" />
            </h:form>

CreateUserView

@ManagedBean(name = "createUserView")
@RequestScoped
public class CreateUserView {

    private UserServices userSerivces;
    private User newUser;

    @ManagedProperty(value="#{organizationBean}") 
    private OrganizationBean organizationBean;

    public CreateUserView() {
        newUser = new User();
        userSerivces = new UserServices();
    }

    public void createNewUser() {       
        userSerivces.createNewUser(newUser);
    }

    // Getters and Setters
}

OrganizationBean

@ManagedBean(name = "organizationBean")
@RequestScoped
public class OrganizationBean {

    private List<Organization> allOrganizations;
    private OrganizationServices orgServices;

    public OrganizationBean() {
        orgServices = new OrganizationServices();
        allOrganizations = orgServices.retrieveAllOrganizations();
    }

    // Getters and Setters
}

这里的问题是,当我在辅助bean中引用newUser对象时,organizationId值为null。

我认为这是因为OrganizationBean(原因是命名,重构中的混淆)要么没有为我当前的视图呈现,要么我需要以某种方式注入。

我在CreateUserView支持bean中尝试了一个托管属性,它引用了OrganizationBean,但没有运气。 newUser对象中的organizationID值为null。

我是否需要使用OrganizationBean注入填充CreateUserView bean中的列表,以便它具有可以呈现的自己的列表?

我错过了什么?感到愚蠢。

JSF 2.0

1 个答案:

答案 0 :(得分:0)

问题中,如评论中所述,您的Converter课程没有Organization

您必须拥有它才能知道OrganizationSelectItem匹配的内容。转换器必须类似于:

@FacesConverter(forClass = Organization.class, value = "organizationConverter")
public class OrganizationConverter implements Converter
{

@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String id)
{
    if (StringUtils.isEmpty(id))
    {
        return null;
    }
    // Convert id to an Organizacion
    return organization;
}

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{
    if (o instanceof Organization)
    {
        return ...;//Convert organization to id
    }
    return null;
}

}

然后在selectonemenu

<h:selectOneMenu id="user_organization" disabled="true" value="#{createUserView.newUser.organizationId}" 
                converter="organizationConverter">
相关问题