【问题标题】:user entered data not updated in managed bean用户输入的数据未在托管 bean 中更新
【发布时间】:2012-04-13 06:15:11
【问题描述】:

我有一个 html 页面(editprofile.xhtml),它显示了保存在数据库中的数据。我面临的问题是当我在屏幕上编辑一些数据并单击更新按钮时,编辑的数据不会进入 bean,它为 null 从而导致错误。

<h:form id="editProfileForm">
 <f:facet name="label">
  <h:outputText value="Edit User Profile" />
 </f:facet>             
 <rich:panel header="Edit User Profile" style="font-size:10pt" >        
  <rich:simpleTogglePanel switchType="client" opened="true">
<f:facet name="header">Registration Details</f:facet>
<table>
    <tbody>
        <tr>
            <td>Login Name</td>
            <td>
            <h:inputText size="15" id="loginName" required="true"
                value="#{EditUserProfileBean.loginName}">
             <rich:ajaxValidator event="onblur" />
            </h:inputText>
            </td>
            <td></td>
            <td>Password</td>
            <td><h:inputSecret size="12" id="password" required="true"
                value="#{EditUserProfileBean.password}" >
            <rich:ajaxValidator event="onblur" />
            </h:inputSecret>
            </td>
            <td>Confirm Password</td>
            <td><h:inputSecret size="12" id="confirmpassword" required="true"
                value="#{EditUserProfileBean.confirmpassword}" >
            <rich:ajaxValidator event="onblur" />
            </h:inputSecret>
            </td>
        </tr>
</rich:simpleTogglePanel>  
<h:commandButton id="editProfile" action="#{EditUserProfileBean.saveEditProfileAction}" immediate="true" value="Update Profile" />
  </rich:panel>
 </h:form>

上面的页面在加载时填充了现有数据,但如果我编辑并说更新,则值为 null

【问题讨论】:

  • 你是否在你的bean中为你想要传递的属性定义了setter?这个代码是sn-p的形式吗?提交表单的代码在哪里,还是ajax?
  • 尝试从你的 commandButton 中删除 immediate="true"。
  • 谢谢,它奏效了……但我很惊讶“立即”是如何负责的……
  • 我将此作为答案发布,并附有解释。

标签: java jsf jakarta-ee jsf-2


【解决方案1】:

从你的 commandButton 中移除 immediate="true" 属性。

它跳过了 jsf 应用程序生命周期的 updateModel 阶段,该阶段负责在 bean 中定义的属性上调用 setter,因此不会更新 bean 值。

阅读更多关于how immediate attributes affects JSF lifecycle 的信息。 -@BalusC 于 27/09/2006 撰写

【讨论】:

    【解决方案2】:

    immediate = true 让您无需验证即可提交表单。为了解决你的问题,你这样做。

    在命令按钮的 saveEditProfileAction 中获取当前用户对象,即编辑对象并调用 save 方法..框架将自动调用服务类的更新或插入方法。因此您的服务类必须包含 inser ,更新, 删除方法。

    saveEditProfileAction{
    super.save(getUserObject());
    }
    

    所以在调用 save 方法时,控件将根据表单状态转到服务类的插入或更新方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2013-07-15
      相关资源
      最近更新 更多