【问题标题】:UI components dont update after backing bean value has changed支持 bean 值更改后 UI 组件不更新
【发布时间】:2016-04-30 14:37:24
【问题描述】:
<h:form>
            <table>
                <tr>
                    <td><label>First Name:</label></td>
                    <td><h:outputText value="#{profile.firstName}"
                            rendered="#{not profile.canEdit}" /></td>
                    <td><h:inputText value="#{profile.firstName}"
                            rendered="#{profile.canEdit}" required="true" /></td>
                    <td><h:commandButton value="Edit"
                            action="#{profile.editDetail}" /></td>
                    <td><h:commandButton value="Cancel" type="button"
                            action="#{profile.cancelBtn}" /></td>
                </tr>
                <tr>
                    <td><label>Last Name:</label></td>
                    <td><h:outputText value="#{profile.lastName}"
                            rendered="#{not profile.canEdit}" /></td>
                    <td><h:inputText value="#{profile.lastName}"
                            rendered="#{profile.canEdit}" required="true" /></td>
                    <td><h:commandButton value="Edit"
                            action="#{profile.editDetail}" /></td>
                    <td><h:commandButton value="Cancel" type="button"
                            action="#{profile.cancelBtn}" /></td>
                </tr>
                <tr>
                    <td><label>Email:</label></td>
                    <td><h:outputText value="#{profile.email}"
                            rendered="#{not profile.canEdit}" /></td>
                    <td><h:inputText value="#{profile.email}" id="email"
                            rendered="#{profile.canEdit}" required="true" /></td>
                    <td><h:commandButton value="Edit"
                            action="#{profile.editDetail}" /></td>
                    <td><h:commandButton value="Cancel" type="button"
                            action="#{profile.cancelBtn}" /></td>
                    <td><h:message for="email" value="#{profile.errorMessage}"
                            rendered="#{profile.errorMessage ne null}" /></td>
                </tr>
                <tr>
                    <td><label>Password:</label></td>
                    <td><h:outputText value="#{profile.password}"
                            rendered="#{not profile.canEdit}" /></td>
                    <td><h:commandButton value="Edit"
                            action="#{profile.editDetail}" /></td>
                    <td><h:commandButton value="Cancel" type="button"
                            action="#{profile.cancelBtn}" /></td>
                </tr>
                <h:panelGroup rendered="#{profile.canEdit}">

                    <tr>
                        <td><label>Old Password: </label></td>
                        <td><h:inputText value="#{profile.password}" required="true" /></td>
                        <td><h:outputText rendered="#{profile.errorMessage != null}"
                                value="#{profile.errorMessage}"></h:outputText></td>
                    </tr>
                    <tr>
                        <td><label>New Password: </label></td>
                        <td><h:inputSecret value="#{profile.newPassword}"
                                required="true" /></td>
                    </tr>
                    <tr>
                        <td><label>Confirm Password: </label></td>
                        <td><h:inputSecret value="#{profile.confirmPassword}"
                                required="true" /></td>
                        <td><h:outputText
                                rendered="#{profile.confirmPassword != profile.newPassword}"
                                value="Passwords donot match!!"></h:outputText></td>
                    </tr>
                    <tr>
                        <td><h:commandButton action="#{profile.savePassword}"
                                value="Save Password"
                                disabled="#{profile.confirmPassword != profile.newPassword}" /></td>
                        <td><h:commandButton action="#{profile.cancelBtn}" value="Cancel" type="button"/></td>
                    </tr>

                </h:panelGroup>
                <tr>
                    <td><label>Gender</label></td>
                    <td>#{profile.gender}</td>
                </tr>
                <tr>
                    <td><label>City</label></td>
                    <td>#{profile.city}</td>
                </tr>
                <tr>
                    <td><label>State</label></td>
                    <td>#{profile.state}</td>
                </tr>
                <tr>
                    <td><label>Country</label></td>
                    <td>#{profile.country}</td>
                </tr>
                <tr>
                    <td><label>Zip-Code</label></td>
                    <td>#{profile.zipCode}</td>
                </tr>
                <tr>
                    <td><label>Phone Number</label></td>
                    <td>#{profile.phoneNumber}</td>
                </tr>
                <tr>
                    <td><h:commandButton action="#{profile.saveDetails}"
                            disabled="#{profile.canEdit eq 'false'}" value="Save" /></td>
                    <td><h:commandButton action="#{profile.cancelBtn}" type="button"
                            value="Cancel" /></td>
                </tr>
            </table>
        </h:form>

我的支持 bean 的代码 sn-p

public String InitializeValues(){


    customer = (CustomerVO) sessionManager.getSession("CustomerBean");
    System.out.println("inside profilepagecontroller"+"\n"+ customer);
    setFirstName(customer.getFirstName());
    this.setLastName(customer.getLastName());
    this.setEmail(customer.getEmail());
    this.setPassword(customer.getPassword());
    this.setCity(customer.getCity());
    this.setState(customer.getState());
    this.setCountry(customer.getCountry());
    this.setPhoneNumber(customer.getPhoneNumber());
    this.setGender(customer.getGender());
    this.setZipCode(customer.getZipCode());
    this.setCustomerId(customer.getCustomerId());

    return "ProfilePage";

}



public String editDetail(){
    setCanEdit(true);
    setCanSave(true);
    return null;
}

public String cancelBtn(){
    setCanEdit(false);
    return "ProfilePage";
}

我需要的问题和解决方案

  1. 当我单击取消按钮时,canEdit 设置为 false,但是 inputText 不会在屏幕上“取消”渲染。它不呈现 输出文本。
  2. 如何在单击编辑后在编辑模式下仅获取特定字段?(为每个字段创建单独的布尔值 obv 不是 可行)
  3. setter 方法设置支持 bean 的字段值。运行代码后,屏幕上不会显示相同的内容。 “这”得到了一切 我需要显示但未显示在屏幕上的值。

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    当我单击取消按钮时,canEdit 设置为 false,但 inputText 不会在屏幕上“取消”渲染。它不呈现 outputText

    您的 commandButton 的 type="button" 不应该是这种情况。

    单击编辑后,如何在编辑模式下仅获取特定字段?( 为每个字段创建单独的布尔值 obv 不可行)

    呵呵当然可行。我看到您使用的唯一情况是,如果某些组件的验证未通过。然后我想你可以在渲染中尝试#{component.valid}。问题是所有组件在开始时都是有效的,然后无效的将不会被渲染。要解决检查请求是否是回发的问题:

    public boolean isPostback(){
        return FacesContext.getCurrentInstance().isPostback();
    }
    rendered="#{component.valid and not mybean.postback}"
    

    setter 方法设置支持 bean 的字段值。在我跑之后 代码,同样不会显示在屏幕上。 'this' 得到所有 我需要显示但未显示在屏幕上的值。

    我不明白这个问题是您在谈论的大写方法 Initialize 吗?您也应该发布整个 bean,因为它从来没有在这里调用过。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 2023-03-14
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      相关资源
      最近更新 更多