java 如何在没有 onclick 事件的情况下显示 Richfaces 模式窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4563799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:59:08  来源:igfitidea点击:

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

javajsfxhtmlrichfaces

提问by MattSayar

I'm trying to implement a modal window like thisto 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:

唯一的问题是我无法弹出模态窗口,除非有某种 onclick 事件。我尝试使用以下代码:

<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 afteryou click submit and hit the database and get returned to the form to click Submit again.

这个想法是,如果出现错误,支持 bean 将呈现它,并且它会呈现,但只有您单击提交并点击数据库并返回到表单以再次单击提交之后。

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

理想情况下,如果返回 true ,我希望在页面加载时弹出模态窗口backingBean.isError,但我觉得我错过了一些东西来实现这一点。有任何想法吗?

采纳答案by Jigar Joshi

Use the showWhenRenderedattribute:

使用showWhenRendered属性:

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

回答by Renan

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

另一种不使用 backbean 和“错误标志”的方法是使用 FacesMessage

Example

例子

If the db return a error, add a new FacesMessage

如果数据库返回错误,添加一个新的 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.maximumSeveirityto display the error message

正如 org.life.java 所说,使用showWhenRendered, but withfacesContext.maximumSeveirity来显示错误信息

<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

模态面板仅在有至少一条消息要显示时才会显示,并且它会自动显示,您只需要添加您的 FacesMessage

The message can be FacesMessage.SEVERITY_INFO, FacesMessage.SEVERITY_WARN, FacesMessage.SEVERITY_ERRORand FacesMessage.SEVERITY_FATAL

消息可以是FacesMessage.SEVERITY_INFO, FacesMessage.SEVERITY_WARN,FacesMessage.SEVERITY_ERRORFacesMessage.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.

此代码将显示一个带有“标题”和图标的模式,例如 (errorIcon) - 标题下方的错误和消息。