0
votes

I have a form where I create a user. In my form, I have multiple properties for that user (I actually use a User object for the retainment of data on submit to the backing 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
}

The issue here is that when I reference the newUser object in the backing bean, the organizationId value is null.

I assume this is because OrganizationBean (excuse the confusing in naming, refactoring) is either not rendered for my current view or I need to somehow inject.

I've tried a managed property in the CreateUserView backing bean that references the OrganizationBean, but no luck. The organizationID value in the newUser object is null.

Do I need to populate a list in the CreateUserView bean using the OrganizationBean injection, so that it has it's own list it can render?

What am I missing? Feeling foolish.

JSF 2.0

1
Running into an issue is not foolish, can happen to anyone. Not creating a minimal reproducible example when requested in How to Ask sort of is. Not providing one makes helping for us difficultKukeltje
What is the scope type of OrganizationBean ?fena coder
Do you have a converter for Organization?Oscar Pérez
Sorry about that, I will update as soon as I get access to my codeStaticMaine
@OscarPérez - I do not have a converter for that object, no.StaticMaine

1 Answers

0
votes

The problem, as stated in the comments is that you don't have a Converter for your Organization class.

You must have it in order to know what Organization matches every SelectItem. The converter must be something like:

@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;
}

}

And then in your selectonemenu:

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