How do I get a richfaces modal window to display without an onclick event?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 12:00:14
Jigar Joshi

Use the showWhenRendered attribute:

<rich:modalPanel left="auto" top="250" id="waitpanel"  
    showWhenRendered="#{backingBean.isError}" minWidth="733" autosized="true">

Another way to do this w/o using the backbean and a "error flag" is using FacesMessage

Example

If the db return a error, add a new FacesMessage

try {
  (...)
}
catch (Exception e) {
   //If theres a error (db error, java error..) or a "throw new Exception()" (if your db error doesn't make a exception) add the message...
   FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Error message.");
   FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}

And as org.life.java said, use showWhenRendered, but with facesContext.maximumSeveirity to display the error message

<rich:modalPanel id="messagePanel" showWhenRendered="#{facesContext.maximumSeverity != null}">
   <rich:messages .../> or <h:messages .../>
</rich:modalPanel>

Modal panel will show up only when theres at least one message to be displayed and it will be automatic you just have to add your FacesMessage

The message can be FacesMessage.SEVERITY_INFO, FacesMessage.SEVERITY_WARN, FacesMessage.SEVERITY_ERROR and FacesMessage.SEVERITY_FATAL

And u can change icons and markers according to the message type, example:

<rich:modalPanel id="messagePanel" showWhenRendered="#{facesContext.maximumSeverity != null}">
   <!-- every severity has a ordinal number, im not sure but 0 = info, 1 = warn, 2 = error and 3 = fatal, i guess -->
   <h:panelGrid columns="2" rendered="#{facesContext.maximumSeverity.ordinal == 0}">
      <h:graphicImage value="/images/icons/mini_info.gif"/>
      <h:outputText value="Information" style="color: blue; font-size: 16px;"/>
   </h:panelGrid>

   <h:panelGrid columns="2" rendered="#{facesContext.maximumSeverity.ordinal == 2}">
      <h:graphicImage value="/images/icons/mini_error.gif"/>
      <h:outputText value="Error" style="color: red; font-size: 16px;"/>
   </h:panelGrid>

   <!-- f:facet to change messsages markers -->
   <rich:messages id="mpMessage1">
      <f:facet id="mpErrorMarker" name="infoMarker">
         <h:outputText value="- "/>
      </f:facet>

      <f:facet id="mpErrorMarker" name="errorMarker">
         <h:outputText value="- "/>
      </f:facet>
   </rich:messages>
</rich:modalPanel>

This code will show a modal with a "title" and icon, like (errorIcon) - Error and message below the title.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!