primefaces教程_Primefaces AjaxBehavior和AjaxExceptionHandler组件示例教程

primefaces教程

Different tutorials were provided here have introduced various Primefaces components and the way in which Ajax component could be used for ajaxifying. Using of ajax component won’t be helpful if you don’t look at deeply. This tutorial aimed to help you gather the required information that makes the use of ajax component much easier.

这里提供了不同的教程,介绍了各种Primefaces组件以及Ajax组件可用于进行Ajaxification的方式。 如果您不深入研究,使用ajax组件将无济于事。 本教程旨在帮助您收集使ajax组件的使用更加容易的必需信息。

Besides all of that important information that you will get here about ajax component, following also the life-cycle of the ajax request/response phases as it’s supported by the JSF 2 framework.

除了您将在此处获得的有关ajax组件的所有重要信息之外,还遵循JSF 2框架支持的ajax请求/响应阶段的生命周期。

Ajax behavior was introduced firstly by the jsf 2 specification, and Primefaces is one of those libraries built on, so you will not find a big difference between standard jsf ajax component f:ajax and Primefaces ajax component p:ajax except the latter used with the Primefaces components.

Ajax行为最初是由jsf 2规范引入的,Primefaces是建立在这些库中的库之一,因此您不会在标准jsf ajax组件f:ajax和Primefaces ajax组件p:ajax之间发现很大的区别,除了后者与Primefaces组件。

This tutorial assumed that you’re using Eclipse IDE and Maven building tool for developing the suggested samples below. In case you’re never using it, it’s good for you looking into Primefaces beginners tutorial that help you doing that easily.

本教程假定您正在使用Eclipse IDE和Maven构建工具来开发以下建议的示例。 如果您从未使用过它,那么对研究Primefaces初学者教程很有帮助 ,它可以帮助您轻松地做到这一点。

Ajax生命周期 (Ajax Lifecycle)

Since JSF 2 release, ajax request/response becomes supported by the JSF lifecycle intuitively. No additional phases added for the original well-known JSF lifecycle, rather the same lifecycle has been evolved to serve the Asynchronous JavaScript And XML (Ajax) request/response mechanism. This tutorial isn’t intended for covering all details of ajax behavior that built-in originally. Keeping you aware of the original mechanism that was provided by the JSF implementation for ajaxify is the goal of this tutorial. More detailed information would be provided later in a specific JSF 2 tutorial.

从JSF 2发布以来,JSF生命周期直观地支持ajax请求/响应。 没有为原始的著名JSF生命周期添加任何其他阶段,而是已经演化出相同的生命周期来服务于异步JavaScript和XML(Ajax)请求/响应机制。 本教程并非旨在涵盖最初内置的Ajax行为的所有详细信息。 本教程旨在使您了解JSF实现为ajaxify提供的原始机制。 稍后将在特定的JSF 2教程中提供更多详细信息。

The below figures depict you which phases are executed for handling the ajax request and which ones for a response.

下图描述了执行Ajax请求的哪些阶段以及响应的阶段。

Request Phases

请求阶段

Response Phases

响应阶段

JSF 2.0, splits the jsf lifecycle into two parts: execute (where components are executed) and render. Any given ajax request you can specify your executed components as well as those rendered.

JSF 2.0将jsf生命周期分为两部分:执行(执行组件)和渲染。 任何给定的ajax请求都可以指定执行的组件以及渲染的组件。

Primefaces had compliant with these parts with minor changes here, instead of using execute and render attributes associated with f:ajax to indicate which components should be executed and rendered respectively, they become process and update attributes associated this time with p:ajax component.

Primefaces在这里做了些微改动就符合这些部分,而不是使用与f:ajax关联的executerender属性来指示应分别执行和渲染哪些组件,这次它们成为与p:ajax组件关联的处理更新属性。

The main goal of Ajax is to process the partial form rather than processing the whole form. Using ajax help you submitting those components mentioned in the process while the only components mentioned in the update are rendered and modified. At the other side, using a normal request/response, the whole form has been processed and updated per every request/response cycle which may cause a performance headache or end user uncomfortable issues.

Ajax的主要目标是处理部分表单而不是处理整个表单。 使用ajax可以帮助您提交流程中提到的那些组件,而呈现和修改更新中提到的唯一组件。 另一方面,使用正常的请求/响应,整个表单已在每个请求/响应周期中进行处理和更新,这可能会导致性能头痛或最终用户不舒服的问题。

AjaxBehavior基本信息 (AjaxBehavior Basic Info)

Tagajax
Behavior Idorg.primfaces.component.AjaxBehavior
Behavior Classorg.primefaces.component.behvior.ajax.AjaxBehavior
标签 阿贾克斯
行为编号 org.primfaces.component.AjaxBehavior
行为类别 org.primefaces.component.behvior.ajax.AjaxBehavior

AjaxBehavior属性 (AjaxBehavior Attributes)

NameDefaultTypeDescription
listenernullMethdExprMethod to process in partial request
immediatefalsebooleanBoolean value that determines the phaseId, when true actions are processed at apply_request_values, when false at invoke_application phase.
asyncfalsebooleanWhen set to true, ajax requests are not queued.
processnullStringComponent(s) to process in partial request.
updatenullStringComponent(s) to update with ajax.
onstartnullStringCallback to execute before ajax request is begins.
oncompletenullStringCallback to execute when ajax request is completed.
onsuccessnullStringCallback to execute when ajax request succeeds.
onerrornullStringCallback to execute when ajax request fails.
globaltruebooleanGlobal ajax requests are listened by ajaxStatus component, setting global to false will not trigger ajaxStatus
delaynullStringIf less than delay milliseconds elapses between calls to request() only the most recent one is sent and all other requests are discarded. If this option is not specified, or if the value of delay is the literal string none no delay is used.
partialSubmitfalsebooleanEnables serialization of values belonging to the partially processed components only.
disabledfalsebooleanDisables ajax behavior.
eventnullStringClient side event to trigger ajax request.
resetValuesfalsebooleanIf true, local values of input components to be updated within the ajax request would be reset.
ignoreAutoUpdatefalseBooleanIf true, components which autoUpdate=”true” will not be
名称 默认 类型 描述
听众 空值 方法 部分请求中的处理方法
即时 布尔值 当在apply_request_values处理为真时,在invoke_application阶段为false时,确定phaseId的布尔值。
异步的 布尔值 设置为true时,ajax请求不会排队。
处理 空值 在部分请求中处理的组件。
更新 空值 用ajax更新的组件。
启动时 空值 在ajax请求开始之前执行的回调。
未完成 空值 当ajax请求完成时执行回调。
成功 空值 Ajax请求成功时执行的回调。
错误 空值 当ajax请求失败时执行回调。
全球 真正 布尔值 全局ajax请求被ajaxStatus组件监听,将global设置为false不会触发ajaxStatus
延迟 空值 如果两次对request()的调用之间的延迟时间少于毫秒,则只会发送最近的一次,而所有其他请求都将被丢弃。 如果未指定此选项,或者delay的值是文字字符串,则不使用delay。
部分提交 布尔值 启用仅属于部分处理的组件的值的序列化。
残障人士 布尔值 禁用ajax行为。
事件 空值 客户端事件触发ajax请求。
resetValues 布尔值 如果为true,则将重置ajax请求中要更新的输入组件的本地值。
ignoreAutoUpdate 布尔型 如果为true,则autoUpdate =“ true”的组件将不会

AjaxBehavior入门 (Getting Started With AjaxBehavior)

Using AjaxBehavior is accomplished by attaching the p:ajax component with the component you want to ajaxify, let’s see the most simple sample that you might used ajax component with.

使用AjaxBehavior是通过将p:ajax组件与您要进行ajaxify的组件相连接来完成的,让我们看一下您可能使用过ajax组件的最简单的示例。

Following below sample of input component that associated with p:ajax for ajaxifying the input itself. Ajax request has initiated as soon as the keyup event triggered, meanwhile the output component will be updated accordingly.

下面是与p:ajax相关的输入组件样本,用于对输入本身进行Ajax。 一旦触发keyup事件,Ajax请求就会启动,同时输出组件也会相应地更新。

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>
	<title>AjaxBehavior</title>
</h:head>
<h:form>
	<p:inputText id="input" value="#{ajaxBehaviorManagedBean.message}">
		<p:ajax event="keyup" update="output1 output2"></p:ajax>
	</p:inputText>
	<br/>
	<p:outputLabel id="output1" value="Output A :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
	<br/>
	<p:outputLabel id="output2" value="Output B :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
</h:form>
</html>
package com.journaldev.prime.faces.beans;

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

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

	public String getMessage() {
		return message;
	}

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

As you’ve noticed above:

正如您在上面注意到的:

  1. AjaxBehavior (p:ajax) component has associated with input component to ajaxify. Typically, it couldn’t be used alone.

    AjaxBehavior(p:ajax)组件已与输入组件关联以进行ajaxify。 通常,不能单独使用它。
  2. Each time the input keyup event triggered, an ajax request is sent to the server. Determination of the type of event you would to consider for triggering passed through using of event property.

    每次触发输入keyup事件时,都会向服务器发送一个ajax请求。 通过使用事件属性来确定要考虑触发的事件类型
  3. For all of those components you would to update accordingly once the response received must be mentioned inside update property in a space delimited expression.

    对于所有这些组件,一旦必须在用空格分隔的表达式中的update属性中提及收到的响应,您将进行相应的更新
By default, ajax request triggered based on the component it’s associated with. For input components, the trigger should be done once the onchange event happened. For command components, the trigger should be done once the onclick event happened. It’s applicable for you to override the default behaviors by providing event attribute with the DOM action you prefer. In our case, we’ve provided the keyup which makes the ajax triggering happened once the key is released over input element.
默认情况下,基于与其关联的组件触发ajax请求。 对于输入组件,应在onchange事件发生后执行触发器。 对于命令组件,应在onclick事件发生后执行触发器。 通过为事件属性提供您喜欢的DOM操作,您可以覆盖默认行为。 在我们的例子中,我们提供了keyup,一旦在输入元素上释放了键,就会使ajax触发发生。

Look at the running demonstration below:

看下面的运行演示:

AjaxBehavior的听众 (AjaxBehavior’s Listener)

Ajax component can be associated with a listener. Listener is a method binding that executed every time in which ajax request has triggered. Below a sample for listening of keyup event.

Ajax组件可以与侦听器关联。 侦听器是一种方法绑定,它在每次触发ajax请求时执行。 下面是一个监听keyup事件的示例。

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>
	<title>AjaxBehavior</title>
</h:head>
<h:form>
	<p:inputText id="input" value="#{ajaxBehaviorManagedBean.message}">
		<p:ajax event="keyup" update="output1 output2" listener="#{ajaxBehaviorManagedBean.keyUpListener}"></p:ajax>
	</p:inputText>
	<br/>
	<p:outputLabel id="output1" value="Output A :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
	<br/>
	<p:outputLabel id="output2" value="Output B :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
</h:form>
</html>
package com.journaldev.prime.faces.beans;

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

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

	public String getMessage() {
		return message;
	}

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

	public void keyUpListener(AjaxBehaviorEvent e){
		System.out.println("AjaxBehavior Listener :: "+e.getBehavior()+" :: "+e.getSource());
	}
}

Find out below the output of printed messages into the console at every time the ajax request has initiated. These messages handled by using the listener invocation.

每次启动ajax请求时,请在控制台中查找以下已打印消息的输出。 这些消息通过使用侦听器调用进行处理。

AjaxExceptionHandler (AjaxExceptionHandler)

AjaxExceptionHandler is a utility component for built-in ExceptionHandler. Let’s firstly look at its basic info and attributes.

AjaxExceptionHandler是内置ExceptionHandler的实用程序组件。 首先让我们看一下它的基本信息和属性。

AjaxExceptionHandler基本信息 (AjaxExceptionHandler Basic Info)

TagajaxExpectionHandler
Component Classorg.primefaces.component.ajaxexceptionhandler.AjaxExceptionHandler
Component Typeorg.primefaces.component.AjaxExceptionHandler
Component Familyorg.primefaces.component
标签 ajaxExpectionHandler
组件类别 org.primefaces.component.ajaxexceptionhandler.AjaxExceptionHandler
组件类型 org.primefaces.component.AjaxExceptionHandler
组件族 org.primefaces.component

AjaxExceptionHandler属性 (AjaxExceptionHandler Attributes)

NameDefaultTypeDescription
idnullStringUnique identifier of the component.
renderedtrueBooleanBoolean value to specify the rendering of the component
bindingnullObjectAn el expression that maps to a server side UIComponent instance in a backing bean
onexceptionnullStringClient side callback to execute after a exception with this type occured.
updatenullStringComponents to update after a exception with this type occured
typenullStringException type to handle.
名称 默认 类型 描述
ID 空值 组件的唯一标识符。
呈现 真正 布尔型 布尔值,用于指定组件的呈现
捆绑 空值 目的 El表达式,它映射到支持Bean中的服务器端UIComponent实例
焦虑 空值 发生此类异常后要执行的客户端回调。
更新 空值 发生此类异常后要更新的组件
类型 空值 要处理的异常类型。

AjaxExceptionHandler入门 (Getting Started With AjaxExceptionHandler)

Before Primfaces 5 release, catching of exceptions and errors was very hard and needs a lot of additional work like browser and jsf lifecycle phases debugging especially when it comes to deal with Ajax.

在Primfaces 5发行之前,捕获异常和错误非常困难,并且需要大量额外的工作,例如浏览器和jsf生命周期阶段调试,尤其是在处理Ajax时。

Most of the developers want to use the ajax mechanism were faced circumstances where everything seems accomplished but even though the ajax request doesn’t trigger. Primefaces 5 provides you a powerful ExceptionHandler out of the box featuring support ajax and non-ajax request. p:ajaxExceptionHandler component to handle ajax exceptions at the same page.

大多数开发人员都想使用ajax机制,尽管ajax请求没有触发,但似乎一切都已完成。 Primefaces 5提供了功能强大的ExceptionHandler ,即开即用,具有支持ajax和非ajax请求的功能。 p:ajaxExceptionHandler组件,用于处理同一页面上的ajax异常。

ExceptionHandler and an ElResolver configured is required in the faces configuration file.

Faces配置文件中需要配置ExceptionHandler和ElResolver。

faces-config.xml

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="https://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">
	<application>
		<el-resolver>
			org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
		</el-resolver>
	</application>
	<factory>
		<exception-handler-factory>
			org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
		</exception-handler-factory>
	</factory>
</faces-config>

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>
	<title>AjaxBehavior</title>
</h:head>
<h:form>
	<p:inputText id="input" value="#{ajaxBehaviorManagedBean.message}">
		<p:ajax event="keyup" update="output1 output2" listener="#{ajaxBehaviorManagedBean.keyUpListener}"></p:ajax>
	</p:inputText>
	<br/>
	<p:outputLabel id="output1" value="Output A :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
	<br/>
	<p:outputLabel id="output2" value="Output B :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
	<br/>
	<p:ajaxExceptionHandler id="exception" type="java.lang.NullPointerException"
	                            update="exceptionPanel" onexception="alert('An Exception Occured ::')"/>

	<p:outputPanel id="exceptionPanel" style="width:500px;height:200px;border:1px;font-size:10px;">
		<p:outputLabel value="Exception Type :: #{pfExceptionHandler.type}"></p:outputLabel>
		<p:outputLabel value="Exception Message :: #{pfExceptionHandler.message}"></p:outputLabel>
		<p:outputLabel value="Exception StackTrace :: #{pfExceptionHandler.formattedStackTrace}"></p:outputLabel>
    </p:outputPanel>
</h:form>
</html>
package com.journaldev.prime.faces.beans;

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

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

	public String getMessage() {
		return message;
	}

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

	public void keyUpListener(AjaxBehaviorEvent e){
		// Print out event information
		System.out.println("AjaxBehavior Listener :: "+e.getBehavior()+" :: "+e.getSource());
		// Throw NullPointerException
		throw new NullPointerException("Anonymous Null Pointer");
	}
}

As you’ve noticed above:

正如您在上面注意到的:

  1. Ajax component associated with a listener for event keyup listening.

    与用于事件keyup侦听的侦听器关联的Ajax组件。
  2. The listener will print out the information of the event but it will throw a NullPointerException.

    侦听器将打印出事件的信息,但会抛出NullPointerException
  3. Primefaces exception handler component will detect the exception thrown and it will print out the all information available including the exception stack trace.

    Primefaces异常处理程序组件将检测抛出的异常,并将打印出所有可用信息,包括异常堆栈跟踪。
  4. onexception property used for provide client side alert once the exception has been thrown. It also could be used for accessing Primefaces components via their WidgetVar and asking them doing some additional work like dialog show or hide.

    引发异常后,onexception属性用于提供客户端警报。 它也可用于通过其WidgetVar访问Primefaces组件,并要求他们进行一些其他工作,例如对话框显示或隐藏。
  5. An implicit object pfExceptionHandler are passed to be accessed by the page, providing the all information about the thrown exception: exception, type, message, stackTrace, formattedStackTrace, timestamp and formattedTimeStamp are the all information you may get from the pfExceptionHandler implicit object.

    隐式对象pfExceptionHandler传递给页面以供访问,提供有关引发的异常的所有信息:异常,类型,消息,stackTrace,formattedStackTrace,timestamp和formattedTimeStamp是您可以从pfExceptionHandler隐式对象获得的所有信息。

AjaxExceptionHandler和错误页面 (AjaxExceptionHandler & Error Pages)

ExceptionHandler is integrated with error-page mechanism of Servlet API. At the application startup Primefaces parses the error pages and uses this information to find the appropriate page to redirect to based on the exception type. Now, the same NullPointerException above should be handled through using both of error-page  concept and the implicit object pfExceptionHandler.

ExceptionHandler与Servlet API的 错误页面机制集成在一起。 在应用程序启动时,Primefaces会分析错误页面,并使用此信息根据异常类型找到要重定向到的适当页面。 现在,应该使用error-page概念和隐式对象pfExceptionHandler来处理上面相同的NullPointerException。

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://java.sun.com/xml/ns/javaee" xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="https://java.sun.com/xml/ns/javaee
	https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5" metadata-complete="true">
	<error-page>
		<exception-type>java.lang.NullPointerException</exception-type>
		<location>/exception.xhtml</location>
	</error-page>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>/faces/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
	<context-param>
		<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
		<param-value>client</param-value>
	</context-param>
	<listener>
		<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
	</listener>
</web-app>

faces-config.xml

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="https://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">
	<application>
		<el-resolver>
			org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
		</el-resolver>
	</application>
	<factory>
		<exception-handler-factory>
			org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
		</exception-handler-factory>
	</factory>
</faces-config>

index.xml

index.xml

<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>
	<title>AjaxBehavior</title>
</h:head>
<h:form>
	<p:inputText id="input" value="#{ajaxBehaviorManagedBean.message}">
		<p:ajax event="keyup" update="output1 output2" listener="#{ajaxBehaviorManagedBean.keyUpListener}"></p:ajax>
	</p:inputText>
	<br/>
	<p:outputLabel id="output1" value="Output A :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
	<br/>
	<p:outputLabel id="output2" value="Output B :#{ajaxBehaviorManagedBean.message}"></p:outputLabel>
</h:form>
</html>

exception.xml

exception.xml

<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>
	<title>NullPointerException Handler</title>
</h:head>
<h:form>
	<h2>NullPointerExceptionHandler</h2>
	<p:ajaxExceptionHandler id="exception" type="java.lang.NullPointerException"
	                            update="exceptionPanel" onexception="alert('An Exception Occured ::')"/>

	<p:outputPanel id="exceptionPanel" style="width:500px;height:200px;border:1px;font-size:10px;">
		<p:outputLabel value="Exception Type :: #{pfExceptionHandler.type}"></p:outputLabel>
		<p:outputLabel value="Exception Message :: #{pfExceptionHandler.message}"></p:outputLabel>
		<p:outputLabel value="Exception StackTrace :: #{pfExceptionHandler.formattedStackTrace}"></p:outputLabel>
    </p:outputPanel>
</h:form>
</html>
package com.journaldev.prime.faces.beans;

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

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

	public String getMessage() {
		return message;
	}

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

	public void keyUpListener(AjaxBehaviorEvent e){
		// Print out event information
		System.out.println("AjaxBehavior Listener :: "+e.getBehavior()+" :: "+e.getSource());
		// Throw NullPointerException
		throw new NullPointerException("Anonymous Null Pointer");
	}
}

In order to allow your exception displayed into error-page, you should define the error-page into your web.xml and configured your Ajax exception handler to be used. Following below the result of executing the above sample.

为了使您的异常显示在错误页面中,您应该在web.xml中定义错误页面,并配置要使用的Ajax异常处理程序。 以下是执行上述示例的结果。

As you’ve noticed, custom error-page is used for handling the thrown exception. Implicit pfExceptionHandler is used for providing the displayed information above.

您已经注意到,自定义错误页面用于处理引发的异常。 隐式pfExceptionHandler用于提供上面显示的信息。

摘要 (Summary)

Multiple components were provided here are used the ajax component to Ajaxify their behaviors. This tutorial spots light into the Ajax lifecycle, AjaxBehavior Primfaces component and the mechanism of handling Ajax exceptions either inside the same page or by using the error-page Servlet API methodology. You should download the final project from the below link and check other attributes to learn more.

这里提供了多个组件,使用了ajax组件来对它们的行为进行Ajax化。 本教程将重点介绍Ajax生命周期,AjaxBehavior Primfaces组件以及在同一页面内或使用错误页面Servlet API方法来处理Ajax异常的机制。 您应该从下面的链接下载最终项目,并检查其他属性以了解更多信息。

翻译自: https://www.journaldev.com/3291/primefaces-ajaxbehavior-and-ajaxexceptionhandler-component-example-tutorial

primefaces教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值