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

ε祈祈猫儿з 提交于 2020-01-13 04:43:06

问题


I'm trying to implement a modal window like this to display an error message to the user. I have a page with a form for users to enter their information, then click Submit to add it to a database. If the database returns an error, I want the modal window to pop up with the error message.

The only problem is I can't get the modal window to pop up unless there's some kind of onclick event. I tried using the following code:

<rich:componentControl for="popup" attachTo="submitButton"
                       rendered="#{backingBean.isError}" operation="show"
                       event="onclick"/>

The idea is that the backing bean would render it if there is an error, and it does, but only after you click submit and hit the database and get returned to the form to click Submit again.

Ideally, I want the modal window to pop up when the page loads if backingBean.isError returns true, but I feel like I'm missing something to make that happen. Any ideas?


回答1:


Use the showWhenRendered attribute:

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



回答2:


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.



来源:https://stackoverflow.com/questions/4563799/how-do-i-get-a-richfaces-modal-window-to-display-without-an-onclick-event

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