primefaces_Primefaces消息,消息和低吼组件示例

本文档详细介绍了Primefaces中的消息组件,包括Message、Messages和Growl的基本信息、属性、显示模式和生命周期。Message是JSF消息组件的扩展,用于与特定组件关联的消息显示。Messages用于显示通用消息,而Growl则提供了一种叠加显示FacesMessages的方式,类似于Mac的通知中心。文章还涵盖了如何控制消息的严重性、自动更新、定位和显示部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

primefaces

Messages are normally used for notifying, informing and keep the users aware of the actions that they are achieved. Typically, messages used for displaying information, errors, warnings and so on. Primefaces like all of jsf implementations, provides a different types of components that are used for doing so. Messages, message and growl are the only components get used for this purpose. This tutorial will help you getting featured these components into your application.

消息通常用于通知,通知用户并使他们知道所实现的操作。 通常,用于显示信息,错误,警告等的消息。 像所有jsf实现一样,Primefaces提供了用于执行此操作的不同类型的组件。 消息,消息和咆哮是用于此目的的唯一组件。 本教程将帮助您在应用程序中使用这些组件。

Primefaces消息基本信息 (Primefaces Message Basic Info)

Message is a pre-skinned extended verison of the standard JSF message component.

消息是标准JSF消息组件的预先皮肤扩展的版本。

Tagmessage
Component Classorg.primefaces.component.message.Message
Component Typeorg.primefaces.component.Message
Component Familyorg.primefaces.component
Renderer Typeorg.primefaces.component.MessageRenderer
Renderer Classorg.primefaces.component.message.MessageRenderer
标签 信息
组件类别 org.primefaces.component.message.Message
组件类型 org.primefaces.component.Message
组件族 org.primefaces.component
渲染器类型 org.primefaces.component.MessageRenderer
渲染器类 org.primefaces.component.message.MessageRenderer

Primefaces消息属性 (Primefaces Message Attributes)

NameDefaultTypeDescription
idnullStringUnique identifier of the component.
renderedtrueBooleanBoolean value to specify the rendering of the component, when set to false component will not be rendered.
bindingnullObjectAn el expression that maps to a server side UIComponent instance in a backing bean.
showSummaryfalseBooleanSpecifies if the summary of the FacesMessage should be displayed.
showDetailtrueBooleanSpecifies if the detail of the FacesMessage should be displayed.
fornullStringId of the component whose messages to display.
redisplaytrueBooleanDefines if already rendered messages should be displayed
displaybothStringDefines the display mode.
escapetrueBooleanDefines whether html would be escaped or not.
severitynullStringComma separated list of severities to display only.
stylenullStringInline style of the component.
styleClassnullStringStyle class of the component.
名称 默认 类型 描述
ID 空值 组件的唯一标识符。
呈现 真正 布尔型 布尔值,用于指定组件的呈现,当设置为false时将不呈现组件。
捆绑 空值 目的 一个el表达式,它映射到后备bean中的服务器端UIComponent实例。
显示摘要 布尔型 指定是否显示FacesMessage的摘要。
查看详细 真正 布尔型 指定是否显示FacesMessage的详细信息。
对于 空值 要显示其消息的组件的ID。
重新显示 真正 布尔型 定义是否应显示已渲染的消息
显示 定义显示模式。
逃逸 真正 布尔型 定义是否对html进行转义。
严重程度 空值 以逗号分隔的严重性列表仅显示。
样式 空值 组件的内联样式。
styleClass 空值 组件的样式类。

Primefaces消息入门 (Getting Started With Primefaces Message)

In general, for adding messages into your application you need to add FacesMessage instances into your own FacesContext instance to be rendered at the RenderResponse phase after then. Many of these messages are added manually and at the same time others are added by jsf implementation. When you deal with validation and conversion, a lot of messages are displayed that actually aren’t part of your code. Following example shows you a simple example of validation process that generates an error message that get displayed when submitting a form without filling required input.

通常,要将消息添加到应用程序中,您需要将FacesMessage实例添加到您自己的FacesContext实例中,然后在之后的RenderResponse阶段进行渲染。 其中许多消息是手动添加的,而其他消息是通过jsf实现添加的。 当您进行验证和转换时,会显示很多实际上不属于您的代码的消息。 以下示例向您展示了一个简单的验证过程示例,该示例将生成一条错误消息,该错误消息在提交表单时不填写所需的输入而显示。

index.xhtml

index.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:outputPanel>
		<p:outputLabel value="Typing of your message is mandatory:"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}" required="true"/>
	<p:message id="message" for="input"></p:message>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action" action="#{messageManagedBean.doSomeAction}" update="input message"></p:commandButton>
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String doSomeAction(){
		return "";
	}
}

Primefaces Message - Validation Error - Initial View
Here’s detailed explanation for the above code:

Primefaces消息-验证错误-初始视图
以下是上述代码的详细说明:

  • The rendered message isn’t part of your code, it’s queued by the jsf implementation through executing of ProcessValidation phase.

    呈现的消息不是代码的一部分,它由jsf实现通过执行ProcessValidation阶段进行排队。
  • RenderResponse phase is responsible of getting messages displayed.

    RenderResponse阶段负责获取显示的消息。
  • Queuing messages require to pass through jsf lifecycle. Normal starting of jsf lifecycle get done by activating an action.

    排队消息需要通过jsf生命周期。 正常启动jsf生命周期可通过激活操作来完成。
  • To ensure that certain input is required, required attribute must be set to true. ProcessValidation will look at your required components and queuing messages in case some of them are missed up.

    为了确保需要某些输入,必须将required属性设置为true。 如果缺少某些组件,ProcessValidation将查看您所需的组件和排队消息。
  • Message component used mainly for associating specific component with a message. Typically, this message will always be used for displaying all messages for accompanying component.

    消息组件,主要用于将特定组件与消息相关联。 通常,此消息将始终用于显示伴随组件的所有消息。
  • The association between the message and it’s relevant component get achieved by providing for attribute.

    该消息和它的相关组件之间的关联得到通过提供属性来实现的。

Primefaces消息显示模式 (Primefaces Message Display Mode)

Message component has three different displays mode:

消息组件具有三种不同的显示模式:

  • text: Only message text is displayed.

    文字:仅显示消息文字。
  • icon: Only message severity is displayed and message text is visible as a tooltip.

    图标:仅显示消息严重性,并且消息文本作为工具提示可见。
  • both (default): Both icon and text are displayed.

    两者(默认):同时显示图标和文本。

Let’s change the same example introduced before to control which display mode that will be used.

让我们更改前面介绍的相同示例,以控制将使用的显示模式。

index.xhtml

index.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:outputPanel>
		<p:outputLabel value="Typing of your message is mandatory:"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}" required="true"/>
	<p:message id="message" for="input" display="icon"></p:message>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action" action="#{messageManagedBean.doSomeAction}" update="input message"></p:commandButton>
</h:form>
</html>

Primefaces消息基本信息 (Primefaces Messages Basic Info)

Messages is a pre-skinned extended version of the standard JSF messages component.

消息是标准JSF消息组件的预先皮肤扩展版本。

Tagmessages
Component Classorg.primefaces.component.messages.Messages
Component Typeorg.primefaces.component.Messages
Component Familyorg.primefaces.component
Renderer Typeorg.primefaces.component.MessagesRenderer
Renderer Classorg.primefaces.component.messages.MessagesRenderer
标签 讯息
组件类别 org.primefaces.component.messages.Messages
组件类型 org.primefaces.component.Messages
组件族 org.primefaces.component
渲染器类型 org.primefaces.component.MessagesRenderer
渲染器类 org.primefaces.component.messages.MessagesRenderer

Primefaces消息属性 (Primefaces Messages Attributes)

NameDefaultTypeDescription
idnullStringUnique identifier of the component.
renderedtrueBooleanBoolean value to specify the rendering of the component, when set to false component will not be rendered.
bindingnullObjectAn el expression that maps to a server side UIComponent instance in a backing bean.
showSummarytrueBooleanSpecifies if the summary of the FacesMessages should be displayed.
showDetailfalseBooleanSpecifies if the detail of the FacesMessages should be displayed.
globalOnlyfalseStringWhen true, only facesmessages with no clientIds are displayed.
redisplaytrueBooleanDefines if already rendered messages should be displayed
autoUpdatefalseBooleanEnables auto update mode if set true.
fornullStringName of associated key, takes precedence when used with globalOnly.
escapetrueBooleanDefines whether html would be escaped or not.
severitynullStringComma separated list of severities to display only.
closablefalseBooleanAdds a close icon to hide the messages.
stylenullStringInline style of the component.
styleClassnullStringStyle class of the component.
showIcontrueBooleanDefines if severity icons would be displayed.
名称 默认 类型 描述
ID 空值 组件的唯一标识符。
呈现 真正 布尔型 布尔值,用于指定组件的呈现,当设置为false时将不呈现组件。
捆绑 空值 目的 一个el表达式,它映射到后备bean中的服务器端UIComponent实例。
显示摘要 真正 布尔型 指定是否应显示FacesMessages的摘要。
查看详细 布尔型 指定是否显示FacesMessages的详细信息。
globalOnly 为true时,仅显示没有clientId的facemessage。
重新显示 真正 布尔型 定义是否应显示已渲染的消息
自动更新 布尔型 如果设置为true,则启用自动更新模式。
对于 空值 关联键的名称,与globalOnly一起使用时优先。
逃逸 真正 布尔型 定义是否对html进行转义。
严重程度 空值 以逗号分隔的严重性列表仅显示。
可关闭 布尔型 添加一个关闭图标以隐藏消息。
样式 空值 组件的内联样式。
styleClass 空值 组件的样式类。
showIcon 真正 布尔型 定义是否显示严重性图标。

Primefaces消息入门 (Getting Started With Primefaces Messages)

When it comes to use p:messages, it’s important to know that this component is used for displaying general messages that are not belong for specific controls in the page. Following sample shows you how can use p:messages for displaying general message.

在使用p:messages时,重要的是要知道此组件用于显示不属于页面中特定控件的常规消息。 以下示例显示了如何使用p:messages显示常规消息。

index2.xhtml

index2.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:messages id="messages"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action"
			action="#{messageManagedBean.doSomeAction}" update="messages"></p:commandButton>
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message ="";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String doSomeAction(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Empty value isn't accepted","Empty value isn't accepted"));
		}
		else if(this.message.equals("") == false){
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "You entered value","You entered value"));
		}
		return "";
	}
}

Here’s detailed clarifications for what’s happened previously:

这是以前发生的情况的详细说明:

  • Messages component used mainly for general message coverage.

    消息组件主要用于常规消息覆盖。
  • You can add a message by creating an instance of FacesMessage that’s comprised from message’s severity, message detail section and message summary section. After finish the creation of message, it’s required for displaying adding it into your FacesContext. RenderResponse will display it into your page.

    您可以通过创建FacesMessage实例来添加消息,该实例由消息的严重性,消息详细信息部分和消息摘要部分组成。 创建完消息后,需要进行显示才能将其添加到FacesContext中。 RenderResponse会将其显示在您的页面中。

严重等级 (Severity Level)

In the previous explored example, you’ve provided two messages with error severity that are rendered after than into your page. It’s important to know that you can control which type of these messages your p:messages component would display. By providing severity attribute with comma separated info, warn, error, fatal values, you are getting controlled those displayed messages.

在上一个示例中,您提供了两个错误严重性消息,这些消息在页面之后而不是页面之后呈现。 重要的是要知道您可以控制p:messages组件将显示哪种消息。 通过为严重性属性提供逗号分隔的信息, 警告,错误,致命值,您可以控制那些显示的消息。

index3.xhtml

index3.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:messages id="messages" severity="fatal,info,warn"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action" 
			action="#{messageManagedBean.doSomeAction}" update="messages"></p:commandButton>
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message ="";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String doSomeAction(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message","Error Message"));
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fatal Message","Fatal Message"));
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_WARN, "WARN Message","WARN Message"));
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO Message","INFO Message"));			
		}		
		return "";
	}
}

自动更新 (AutoUpdate)

If you’ve explored the all examples provided before, you must notice that the p:commandButton has updated the message/messages component asynchronously. You can avoid like that arrangement and especially for those pages that have a hierarchical establishment. Let we have a template page that contains a messages component that would be used for dispalying all general messages your application has thrown.

如果您浏览了之前提供的所有示例,则必须注意p:commandButton已异步更新了message / messages组件。 您可以避免这种安排,尤其是对于具有分层结构的页面。 让我们有一个模板页面,其中包含一个消息组件,该组件将用于消除应用程序引发的所有常规消息。

index4.xhtml

index4.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:messages id="messages" autoUpdate="true"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action" 
			action="#{messageManagedBean.doSomeAction}"></p:commandButton>
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message ="";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String doSomeAction(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message","Error Message"));		
		}		
		return "";
	}
}
  • Developed command action hasn’t provided update attribute. Even though update attribute isn’t there, yet the message has been displayed cause the autoUpdate is used by the messages component itself.

    开发的命令操作未提供update属性。 即使没有update属性,消息仍已显示,原因是消息组件本身使用了autoUpdate

定向消息 (Targetable Messages)

Displaying messages can be controlled to be viewed using specific messages component. Let’s use two different messages components {A and B} and two different inputs components {1 and 2}. For input number 1 the messages would be displayed against messages A and for 2 messages B will be used. Following example shows you the impact of like that usage.

可以控制显示的消息以使用特定的消息组件进行查看。 让我们使用两个不同的消息组件{A和B}和两个不同的输入组件{1和2}。 对于输入数字1,将针对消息A显示消息,对于2,将使用消息B。 以下示例向您展示了类似用法的影响。

index5.xhtml

index5.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:messages for="input1" id="messagesA"/>
	<p:messages for="input2" id="messagesB"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input1" value="#{messageManagedBean.message}"/>
	<h:inputText id="input2" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeActionOne}" update="messagesA messagesB"></p:commandButton>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action Two" 
			action="#{messageManagedBean.doSomeActionTwo}" update="messagesA messagesB"></p:commandButton>			
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message ="";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String doSomeActionOne(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage("form:input1", 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message For Input1","Error Message For Input1"));		
		}		
		return "";
	}
	public String doSomeActionTwo(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage("form:input2", 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message For Input2","Error Message For Input2"));		
		}		
		return "";
	}	
}
  • Featuring Target Messages requires associate your messages component with a component using for attribute and providing clientId for all of those messages that are added into FacesContext.

    要使用目标消息,需要将您的消息组件与使用for属性关联的组件相关联并为所有添加到FacesContext中的消息提供clientId

Note that the jsf implementation has assigned its components a unique identifiers. Those identifiers take the form of FormId:componentId. You can disable this identification by providing prependId to false against form component. So every component will be actually identified by using only its componentId and for those that are not identified, they will be identified using random identification like j_id4.

请注意,jsf实现已为其组件分配了唯一的标识符。 这些标识符采用FormId:componentId的形式。 您可以通过为表单组件提供prependId为false来禁用此标识。 因此,实际上将仅通过使用其componentId来标识每个组件,对于未标识的组件,将使用诸如j_id4的随机标识来标识它们。

Primefaces Growl基本信息 (Primefaces Growl Basic Info)

Growl is based on the Mac’s growl notification widget and used for displaying FacesMessages in an overlay and just like message and messages components.

Growl基于Mac的咆哮通知窗口小部件,用于像叠加消息和消息组件一样,以叠加方式显示FacesMessages。

TagGrowl
Component Classorg.primefaces.component.growl.Growl
Component Typeorg.primefaces.component.Growl
Component Familyorg.primefaces.component
Renderer Typeorg.primefaces.component.GrowlRenderer
Renderer Classorg.primefaces.component.growl.GrowlRenderer
标签 低吼
组件类别 org.primefaces.component.growl.Growl
组件类型 org.primefaces.component.Growl
组件族 org.primefaces.component
渲染器类型 org.primefaces.component.GrowlRenderer
渲染器类 org.primefaces.component.growl.GrowlRenderer

Primefaces低吼属性 (Primefaces Growl Attributes)

NameDefaultTypeDescription
idnullStringUnique identifier of the component
renderedtrueBooleanBoolean value to specify the rendering of the component, when set to false component will not be rendered.
bindingnullObjectAn el expression that maps to a server side UIComponent instance in a backing bean
stickyfalseBooleanSpecifies if the message should stay instead of hidden automatically.
showSummarytrueBooleanSpecifies if the summary of message should be displayed.
showDetailfalseBooleanSpecifies if the detail of message should be displayed.
globalOnlyfalseBooleanWhen true, only facesmessages without clientids are displayed.
life6000IntegerDuration in milliseconds to display non-sticky messages.
autoUpdatefalseBooleanSpecifies auto update mode.
redisplaytrueBooleanDefines if already rendered messaged should be displayed.
fornullStringName of associated key, takes precedence when used with globalOnly.
escapetrueBooleanDefines whether html would be escaped or not.
severitynullStringComma separated list of severities to display only.
名称 默认 类型 描述
ID 空值 组件的唯一标识符
呈现 真正 布尔型 布尔值,用于指定组件的呈现,当设置为false时将不呈现组件。
捆绑 空值 目的 El表达式,它映射到支持Bean中的服务器端UIComponent实例
布尔型 指定是否保留消息而不是自动隐藏消息。
显示摘要 真正 布尔型 指定是否应显示消息摘要。
查看详细 布尔型 指定是否显示消息的详细信息。
globalOnly 布尔型 为true时,仅显示没有客户端id的facemessage。
生活 6000 整数 显示非粘性消息的持续时间(以毫秒为单位)。
自动更新 布尔型 指定自动更新模式。
重新显示 真正 布尔型 定义是否应显示已渲染的消息。
对于 空值 关联键的名称,与globalOnly一起使用时优先。
逃逸 真正 布尔型 定义是否对html进行转义。
严重程度 空值 以逗号分隔的严重性列表仅显示。

Primefaces Growl入门 (Getting Started With Primefaces Growl)

Growl hasn’t vary differ from these discussed messages components earlier, so you can rely on them for providing Targetable Messages & Severity Levels options. Following example shows you the most simple example that you may get for Growl component.

Growl与前面讨论的消息组件没有什么不同,因此您可以依靠它们来提供“目标消息和严重性级别”选项。 以下示例向您展示了有关Growl组件的最简单示例。

index6.xhtml

index6.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:growl id="message"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeAction}" update="message"></p:commandButton>	
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message ="";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String doSomeAction(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message Displayed Growl","Error Message Displayed Growl"));		
		}		
		return "";
	}
}

Primefaces消息的生命周期 (Lifetime of Primefaces messages)

As each message will be displayed for 6000 ms and then hidden, you can control Growl message to be sticky, that’s meaning it’ll never be hidden automatically.

由于每条消息将显示6000毫秒然后隐藏,因此您可以控制Growl消息具有粘性,这意味着它将永远不会被自动隐藏。

index7.xhtml

index7.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:growl id="message" sticky="true"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeAction}" update="message"></p:commandButton>	
</h:form>
</html>

If you wouldn’t your Growl message to be work in a sticky manner, you can also control the duration of displaying messages by tuning of life attribute.

如果您不想让Growl消息以粘性方式起作用,则还可以通过调整life属性来控制显示消息的持续时间。

index8.xhtml

index8.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:growl id="message" life="2000"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeAction}" update="message"></p:commandButton>	
</h:form>
</html>

Primefaces咆哮消息定位 (Primefaces growl messages Positioning)

You can also control the position that Growl message has seen into. By default Growl is positioned at the top right corner, position can be controlled with CSS selector called ui-growl.

您还可以控制Growl消息已进入的位置。 默认情况下,Growl位于右上角,可以使用名为ui-growl的 CSS选择器控制位置。

index9.xhtml

index9.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
	<style>
		.ui-growl {
			left:700px;
		}
	</style>
</h:head>
<h:form id="form">
	<p:growl id="message"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeAction}" update="message"></p:commandButton>	
</h:form>
</html>

转义 (Escaping)

For all of Primefaces messages components (message, messages and growl), they are defaulted to escape all of the html content. In case you need to display html via Primefaces messages components set escape to false.

对于所有Primefaces消息组件(消息,消息和咆哮声),默认情况下它们将转义所有html内容。 如果您需要通过Primefaces消息显示html组件,请将转义设置为false。

index10.xhtml

index10.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:messages id="message" escape="false"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeAction}" update="message"></p:commandButton>	
</h:form>
</html>

MessageManagedBean.java

MessageManagedBean.java

package com.journaldev;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MessageManagedBean {
	private String message ="";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String doSomeAction(){
		if(this.message.equals("")){
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "<i>Error Message Displayed</i>","<i>Error Message Displayed</i>"));		
		}		
		return "";
	}
}

详细和摘要消息部分 (Detail & Summary Message Parts)

Displaying of messages’ parts can be controlled, so you can choose which part of message you need to display. All of FacesMessage contains for Summary and Detail parts that are provided once the message has been added into your FacesContext.

消息部分的显示可以控制,因此您可以选择需要显示的消息部分。 一旦将消息添加到FacesContext中,所有FacesMessage都包含“摘要”和“详细信息”部分。

All of Primefaces’ messages components are defaulted to render the summary part. As you can provide showSummary and showDetail for displaying both parts of FacesMessage.

默认情况下,所有Primefaces的消息组件都将呈现摘要部分。 您可以提供showSummaryshowDetail来显示FacesMessage的两个部分。

index11.xhtml

index11.xhtml

<html xmlns="https://www.w3.org/1999/xhtml"
	xmlns:ui="https://java.sun.com/jsf/facelets"
	xmlns:h="https://java.sun.com/jsf/html"
	xmlns:f="https://java.sun.com/jsf/core"
	xmlns:p="https://primefaces.org/ui">
<h:head>
	<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
	<p:messages id="message" showDetail="true" showSummary="true" escape="false"/>
	<p:outputPanel>
		<p:outputLabel value="Typing of your preferred technical site"></p:outputLabel>
	</p:outputPanel>
	<h:inputText id="input" value="#{messageManagedBean.message}"/>
	<p:commandButton value="Execute JSF Lifecycle - Invoke Action One" 
			action="#{messageManagedBean.doSomeAction}" update="message"></p:commandButton>	
</h:form>
</html>

Primefaces消息低吼摘要 (Primefaces Messages Growl Summary)

Messages are heavily used inside wide range of applications that get published. Primefaces provides you a large scale of these components that can be used for informing, notifying and displaying informative text within your application. Contribute us by commenting below and find the source code.

消息在发布的各种应用程序中大量使用。 Primefaces为您提供了大量的这些组件,可用于在应用程序内通知,通知和显示信息性文本。 通过在下面发表评论来贡献我们,并找到源代码。

翻译自: https://www.journaldev.com/3851/primefaces-message-messages-growl-components-example

primefaces

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值