有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解决Primefaces中的“p:消息不工作”?

我正在构建一个表单,允许用户输入数据库连接参数。输入参数后 用户可以测试(btnTester)是否可以使用其参数建立连接

在所有情况下,都会为用户生成一条消息。下面是来自支持bean代码的连接尝试失败的示例:

(...)
addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection failed.", t.getLocalizedMessage()));
(...)

这是表格代码。我希望邮件显示在p:邮件中。不幸的是,什么也没有发生。按钮后不显示任何消息(连接成功或未成功):\ 即使全局属性设置为false或true

<h:form prependId="false">
(...)
  <h:panelGrid>                 
    <!-- Other form components here -->
    <f:facet name="footer">
      <p:commandButton
          id="btnTester"
          value="Tester"
          actionListener="#{assistantCreationSourcesBean.testerConnexionBase}"
          update="msgTester" />
          <p:message id="msgTester" for="btnTester" />
    </f:facet>
 </h:panelGrid>
</h:form>

我错过了什么


共 (6) 个答案

  1. # 1 楼答案

    我的猜测是您尝试更新尚未渲染的组件。试试这个

    使用Firefox Firebug并检查html源代码以查看组件idmsgTester是否存在。如果不是,则说明更新无法工作的原因。您无法更新不存在的内容。如何解决这个问题,您可以为此创建一个包装器,并更新您的包装器。你的包装应该是它一直存在的东西,比如h:form。如果您不想更新整个表单,那么可以尝试<p:outputPanel id="wrapper">。所以你的代码看起来像这样

    <h:form>
        ...
        <p:outputPanel id="wrapper">
            <p:commandButton ... update="wrapper"/>
            <p:message />
        </p:outputPanel>
    </h:form>
    

    试试这个,让我知道它是否有效

  2. # 2 楼答案

    找到了一个解决方案,可以完美地处理Prime face中的消息

    与你分享想法,即使你通过咆哮解决了问题

    这是解决方案

    AddAddMessage有两个参数,一个UIComponent客户端Id和FacesMessage。facesMessage显示或定义消息,组件子Id显示组件所在的位置。 在这里,您错过了组成部分。 为了使其工作,您应该将UIComponent与bean类绑定。 这样它就可以找到组件

    现在做如下的事情

    Define a UIComponent in bean class

    Bind it with the corresponding command Button

    and get the Id From the component in the bean Class

    您的代码必须这样更改

    >  private UIComponent component;
    
    (...)
    addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,
    "Connection failed.", t.getLocalizedMessage()));
    (...)
    

    在xhtml文件中,必须绑定UITML组件

    <h:form prependId="false">
    (...)
      <h:panelGrid>                 
        <!-- Other form components here -->
        <f:facet name="footer">
          <p:commandButton
              id="btnTester"
              value="Tester"
              actionListener="#{assistantCreationSourcesBean.testerConnexionBase}"
              update="msgTester" 
              binding="#{assistantCreationSourcesBean.component}"/>
              <p:message id="msgTester" for="btnTester" />
        </f:facet>
     </h:panelGrid>
    </h:form>
    

    现在消息将起作用。。!!对我来说,它非常有效:)

  3. # 3 楼答案

    <p:commandButton>上缺少一个update属性,该属性指定要更新的<p:message>组件的ID

    您应该给出消息组件和ID,并在commandButton的update中指定它

  4. # 4 楼答案

    我认为你的问题在于:

    addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection failed.", t.getLocalizedMessage()));
    

    我想你是在给FacesContext.addMessage()打电话。第一个参数是组件id字符串。如果将其设置为null,则强制消息为全局消息。您定义的<p:message>组件被设置为for="btnTester",因此它将仅显示组件id与btnTester组件id匹配的消息

    除了addMessage()的Javadoc之外:

    Append a FacesMessage to the set of messages associated with the specified client identifier, if clientId is not null. If clientId is null, this FacesMessage is assumed to not be associated with any specific component instance.

    Link to FacesMessage Javadoc for addMessage() method

  5. # 5 楼答案

    我用咆哮代替信息来解决我的问题

  6. # 6 楼答案

    此处的“for”属性不适用于p:message,请改用p:messages。 <p:messages id="txtname" showIcon="false" showDetail="true" showSummary="false" redisplay="false" for="someKey" display="text"/>