How to get richfaces text box to display without onclick event?

I am trying to implement a modal window like this in order to display an error message to the user. I have a page with a form for users to enter their information, and then click "Submit" to add it to the database. If the database returns an error, I want the modal window to appear with an error message.

The only problem is that I cannot get the mod window to pop up if there is no 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 backup bean will display it if there is an error, and it is executed, but only after you click submit and click on the database and return to the form to click "Submit" again.

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

+3
source share
2 answers

Use attribute showWhenRendered:

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

Another way to do this without using backbean and the “error flag” is to use FacesMessage

Example

If db returns an 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);
}

org.life.java, showWhenRendered, facesContext.maximumSeveirity

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

, , , FacesMessage

FacesMessage.SEVERITY_INFO, FacesMessage.SEVERITY_WARN, FacesMessage.SEVERITY_ERROR FacesMessage.SEVERITY_FATAL

, :

<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>

"" , (errorIcon). .

+3

Source: https://habr.com/ru/post/1782791/


All Articles