1

I'm having some problems with bean initialization. I have an "edit" form to update some user data. The user is previously created in the database and I retrieve successfuly the data and put it in the form.

This is the form with id parameter 1 (/cms/admin/users/edit.jsf?id=1):

<h:panelGrid styleClass="general-form" columns="3" id="ajaxForm">
<h:outputLabel for="name" value="#{cms['users.add.name']}" />
<p:inputText required="true" id="name"
    value="#{editUserController.user.name}"
    label="#{cms['users.add.name']}">
    <f:validateLength minimum="3" />
</p:inputText>
<p:message for="name" />

<h:outputLabel value="#{cms['users.add.lastname']}" />
<p:inputText id="lastname" label="#{cms['users.add.lastname']}"
    required="true" value="#{editUserController.user.lastname}">
    <f:validateLength minimum="3" />
</p:inputText>
<p:message for="lastname" />

<h:outputLabel value="#{cms['users.add.username']}" />
<p:inputText id="username" required="true"
    label="#{cms['users.add.username']}"
    value="#{editUserController.user.username}">
    <f:validateLength minimum="3" />
</p:inputText>

<p:message for="username" />

<h:outputLabel value="#{cms['users.add.password']}" />
<p:password id="password" required="true"
    value="#{editUserController.user.password}"
    promptLabel="#{cms['users.error.enterPassword']}"
    weakLabel="#{cms['users.error.weakPassword']}"
    goodLabel="#{cms['users.error.goodPassword']}"
    strongLabel="#{cms['users.error.strongPassword']}"
    binding="#{password}" label="#{cms['users.add.password']}">
    <f:validateLength minimum="6" />
</p:password>
<p:message for="password" />

<h:outputLabel value="#{cms['users.add.confirmPassword']}" />
<p:password id="passwordConfirm" required="true"
    value="#{editUserController.confirmPassword}"
    promptLabel="#{cms['users.error.enterPassword']}"
    weakLabel="#{cms['users.error.weakPassword']}"
    goodLabel="#{cms['users.error.goodPassword']}"
    strongLabel="#{cms['users.error.strongPassword']}"
    label="#{cms['users.add.confirmPassword']}">
    <f:validateLength minimum="6" />
    <f:validator validatorId="passwordValidator" />
    <f:attribute name="password" value="#{password.value}" />
</p:password>
<p:message for="passwordConfirm" />

<h:outputLabel value="#{cms['users.add.active']}" />
<h:selectOneMenu value="#{editUserController.user.active}">
    <f:selectItem itemValue="true" itemLabel="Sí" />
    <f:selectItem itemValue="false" itemLabel="No" />
</h:selectOneMenu>
<h:outputText value="" />

<h:outputLabel value="#{cms['users.add.role']}" />
<h:selectOneMenu value="#{editUserController.securityRole}">
    <f:selectItems value="#{editUserController.securityRoles}" />
</h:selectOneMenu>
<h:outputText value="" />

<h:outputText value="" />
<p:commandButton action="#{editUserController.saveUser}"
    style="margin-top:20px;" ajax="false"
    value="#{cms['general.save']}"></p:commandButton>

This is the Managed Bean (EditUserController.java):

public class EditUserController extends GeneralController implements
    Serializable {

private static final long serialVersionUID = 1L;

@ManagedProperty("#{param.id}")
private Integer id;

private UserService userService;

private SecurityRoleService securityRoleService;

private User user;

private String confirmPassword;

private Map<String, SecurityRole> securityRoles;

private String securityRole;

@PostConstruct
public String checkUser() {
    try {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        this.id = Integer.parseInt(facesContext.getExternalContext().getRequestParameterMap().get("id"));

        setUser(userService.findById(id));
        Map<String, SecurityRole> secRoles = new ListUtils<String>().toMap(
                securityRoleService.findAll(0, 30, "", ""), "name");

        setSecurityRoles(secRoles);

        setSecurityRole(user.getSecurityRole().getId().toString());

        return "edit";
    } catch (EntityNotFoundException e) {
        return FATAL;
    } catch (NoSuchMethodException e) {
        return FATAL;
    } catch (InvocationTargetException e) {
        return FATAL;
    } catch (IllegalAccessException e) {
        return FATAL;
    }
}

public String saveUser() {
    try {   
        System.out.println("User saved!!");
        return SUCCESS;
    } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage(
                null,
                new FacesMessage(MessageProvider.getMessageProvider()
                        .getValue("cms", "general.error")));
        return ERROR;
    }
}

//getters and setters ...

With the @Postconstruct method I initialize the user variable according to the id param. Then the form is displayed correctly with all data prefilled. But when I click in the commandButton I get an exception:

Error creating bean with name 'editUserController': Invocation of init method failed; nested exception is java.lang.NumberFormatException: null

I understand where is the error. In the @Postconstruct method the variable id has not been "initialized" because there is not any id parameter in the request. I know I can solve this scoping the bean to session or application, but it does not make any sense to have the bean there just to perform this simple action.

How can I do this simple form? I just want to get an id parameter, show a form with prefilled data then give the posibility to change the data an finally submit the form to save the new values into the DB

Thank you all

jacruzca
  • 224
  • 1
  • 3
  • 10

1 Answers1

2

If putting the bean in the view scope and using <f:viewParam> is not an option, then you'd need to pass the parameter back to the next request by including it as <f:param> in the UICommand component:

<p:commandButton action="#{editUserController.saveUser}"
    style="margin-top:20px;" ajax="false"
    value="#{cms['general.save']}">
    <f:param name="id" value="#{editUserController.id}" />
</p:commandButton>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That's a great explanation. With f:param I resolved my problem. Now I better understand the scopes. Thank you so much! – jacruzca Nov 22 '11 at 02:33