JSF2 Managed Bean变量初始化

时间:2011-11-21 18:21:50

标签: jsf-2 initialization

我在bean初始化时遇到了一些问题。我有一个“编辑”表单来更新 一些用户数据。用户先前已在数据库中创建 我成功检索数据并将其放入表单中。

这是id参数为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>

这是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 ...

使用@Postconstruct方法,我根据id param 初始化 user 变量。 然后正确显示表单,并预先填写所有数据。但是当我点击时 commandButton我得到一个例外:

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

我明白错误在哪里。在@Postconstruct方法中,变量 id 没有 已被“初始化”,因为请求中没有任何 id 参数。我知道我可以解决这个问题 将bean定义为会话或应用程序,但将bean放在那里没有任何意义 只是为了执行这个简单的动作。

我该怎么做这个简单的表格?我只想获得一个id参数,显示一个带有预填充数据的表单 然后给出更改数据的可能性,最后提交表单以保存新值  进入DB

谢谢大家

1 个答案:

答案 0 :(得分:1)

如果将bean放在视图范围内并且使用<f:viewParam>不是一个选项,那么您需要将参数作为<f:param>包含在{UICommand中,将参数传递回下一个请求。 1}}组件:

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

另见: