Listen to Ajax requests and respond
When a page uses JavaScript to send an Ajax request to the server, the server needs to process the request. Therefore, the content of this section is mainly on the server side. First, let's take a look at some JSF content-related to phaselistener.
The so-called phase is the JSF request stage. a jsf request has different stages in its lifecycle. When a user sends a request from a page to the server, the JSF request lifecycle begins. The following are the six phases in the famous JSF request lifecycle:
Figure 23 JSF request Lifecycle
Phaselistener is an interface for listening to the JSF request stage. When a class implements this interface and is added to the JSF application, every time the server receives the JSF request, when the request is processed to a certain stage, the listener class will be triggered.
The following is a listener class that monitors custom Ajax requests:
Public class mapupdatephaselistener implements phaselistener
{
Public void afterphase (phaseevent event)
{
Facescontext = event. getfacescontext ();
Map Params = facescontext. getexternalcontext (). getrequestparametermap ();
Webcontext = webutil. getwebcontext (facescontext. getviewroot ());
Webmap = webcontext. getwebmap ();
If ("true". Equals (Params. Get ("mapzoom ")))
{
Uicomponent form = facescontext. getviewroot (). findcomponent ("formmain ");
Vector eventsources = new vector ();
Try
{
Ajaxresponserenderer Renderer = new
Ajaxresponserenderer (facescontext, eventsources, form );
Webextent extent = webmap. getcurrentextent ();
Extent. Expand (0.2 );
Webcontext. Refresh ();
Document Doc = Renderer. renderresponse (facescontext );
Ajaxutil. writeresponse (facescontext, DOC );
}
Catch (exception E)
{
E. printstacktrace ();
}
Finally
{
Facescontext. responsecomplete ();
}
}
}
Public void beforephase (phaseevent event ){}
Public phaseid getphaseid ()
{
Return phaseid. apply_request_values;
}
}
By implementing the getphaseid method, we specify that this class listens to the application request value phase of the JSF request. In this phase, we intercept the request and determine the request parameters. In the previous section, we added the mapzoom attribute to the Request Parameters. here we need to determine whether this attribute exists in the request. If this attribute is true, this indicates that the current request is a custom request that requires the server to process the "5 times larger map" function.
In the process of this request, we can use the webcontext object operation to enlarge the map by 5 times, but here you will find that, after this function is implemented, the refresh method of webcontext is not directly called as before, so it can be displayed to the browser. Why?
For more information about why the refresh method of webcontext can output the results to the browser, you can review the previous webcontext refresh method. Because our requests are not sent through the ADF component, postbackphaselistener will not be triggered (and there is no business logic we need to implement on the server ), here, mapupdatephaselistener needs to output its own Ajax response.
How to construct the Ajax response and output the response to the browser through the writeresponse method of ajaxutil. The above code is clear. The process of constructing the Ajax response is complicated, because we have updated the map here (instead of using tool or command ), therefore, we also need to notify the client of the status of components such as map on the current server to refresh the client (if you do not need to notify the client of relevant information about the ADF component, for example, if you only want to obtain some information through Ajax, it will be relatively simple, as shown in the following section ). By the way, you must pay attention to the order of instantiating the ajaxresponserenderer object, refreshing webcontext, and outputting Ajax responses when constructing your own Ajax response.
Also, you need to use this phaselistener, You need to register this class into the faces-config.xml file, as shown below:
Wuyf. mapupdatephaselistener
Of course, there is also a simple way to use the eclipse menu to define the phaselistener class when creating it, which is much easier than manually modifying the code and configuration file: