【问题标题】:How to show a message in jsf page after completing DB update完成数据库更新后如何在 jsf 页面中显示消息
【发布时间】:2014-05-12 16:11:13
【问题描述】:

我正在使用以下代码来编辑记录的值。并且代码运行良好。

<rich:popupPanel header="Edit Company Region" id="editCompanyRegionPane"
                            domElementAttachment="parent" width="230" height="115">
                            <h:form>
                            <h:panelGrid columns="3" id="editCompanyRegionGrid">
                                <h:inputHidden value="#{CompanyAdminPageModel.editCompanyRegion.companyRegionId}"
                                    id="editcompanyRegionId">
                                </h:inputHidden>
                                <h:message for="editcompanyRegionId" />
                                <h:column>                          
                                <h:outputText value="Name " />
                                <h:inputText value="#{CompanyAdminPageModel.editCompanyRegion.name}" 
                                    id="editCompanyRegionName">
                                </h:inputText>
                                <h:message for="editCompanyRegionName" />
                                </h:column>
                            </h:panelGrid>

                            <h:commandButton value="Update"
                                action="#{CompanyAdminPageModel.companyRegionUpdate}" render="table"
                                oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editCompanyRegionPane')}.hide();}" />
                            <h:commandButton value="Cancel"
                                onclick="#{rich:component('editCompanyRegionPane')}.hide(); return false;" />
                            </h:form>
        </rich:popupPanel>

我想在更新数据库中的值后在同一页面中显示一条消息。例如:当我点击更新按钮时,它会调用 Bean 类的方法,然后更新数据库中的值,关闭弹出面板后,它会在同一页面内显示一条消息(如:更新成功)。

请帮忙。

我使用了以下java代码:

public String updateUser(Long usrId, String firstName, String lastName,
        String loginName, String emailAddr) {
    AllCompanyDAO aDAO = new AllCompanyDAO();
    FacesContext.getCurrentInstance().addMessage("test", new javax.faces.application.FacesMessage("Success"));
    return aDAO.userUpdate(usrId, firstName, lastName, loginName, emailAddr);

}

并遵循jsf代码:

<div class="content container">
        <div style="padding-left:220px;">
             To see the relationships between users and regions <a href="#{appPath}/app/insertion/userRegionInfo.faces" >click here</a> <br/>
             <h:message for="test"></h:message>
        </div>
    </div>

【问题讨论】:

  • 您可以使用FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage("Success")); 向FacesContext 发送消息。并且只需一个h:message 来显示消息。见FacesContext api
  • 你能告诉我,我应该在哪里以及如何在我的代码中使用它?
  • 只需将其放在更新侦听器方法的末尾即可。如果更新成功,将消息添加到 FacesContext。该消息应显示在您拥有h:message 的位置
  • @peeskillet:我已经编辑了我的问题。请看一下。它现在以我真正想要的方式工作。请看一看。谢谢。
  • 如果我使用 FacesContext.getCurrentInstance().addMessage(null, new javax.faces.application.FacesMessage("Success"));然后它在我想要的面板之外显示成功消息。我需要它显示在特定的 div 中。我该怎么做?

标签: database jsf message


【解决方案1】:

companyRegionUpdate()末尾添加:

  1. 一条消息

    FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("Success"));

其中 test 是对 &lt;h:message&gt; JSF 组件的绝对引用。

  1. 在您的 &lt;h:commandButton&gt; 中添加 Ajax 支持

<h:commandButton value="Update"  >
<a4j:support oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editCompanyRegionPane')}.hide();}" action="#{CompanyAdminPageModel.companyRegionUpdate}"  event="onclick" reRender="test, table" />
</h:commandButton>

其中 test 是对 &lt;h:message&gt; JSF 组件的绝对引用。

您可以简单地使用支持 ajax 的 &lt;a4j:commandButton &gt; 组件,而不是向您的 &lt;h:commandButton&gt; 添加 ajax 支持。

【讨论】:

    【解决方案2】:

    我使用以下代码解决了我的问题。 1.在java的companyRegionUpdate方法中使用如下代码。

     FacesContext.getCurrentInstance().addMessage("test", new javax.faces.application.FacesMessage("Successful")); 
    

    然后在我想要的 div 中使用&lt;h:messages /&gt;&lt;h:messages /&gt;&lt;h:message /&gt; 不同。就我而言,&lt;h:messages /&gt; 有效。

    就是这样。 :-)

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 2011-08-15
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2013-07-18
      • 2020-10-08
      相关资源
      最近更新 更多