Start Execution in Foreign Ajax Channel

From Documentation
Revision as of 07:33, 8 July 2022 by Hawk (talk | contribs) ((via JWB))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Start Execution in Foreign Ajax Channel


Employment/Purpose

Here we describe how to start a ZK execution in a foreign Ajax channel. For example, JSF 2 allows developers to send back JavaScript code to update the browser in JSF's Ajax channel.

Bridge

Starting an execution in a foreign Ajax channel is straightforward: invoke Bridge.start(ServletContext, HttpServletRequest, HttpServletResponse, Desktop). Then, you are allowed to access the components, post events and do anything you like. At the end, you invoke Bridge.getResult() to retrieve the JavaScript code snippet and send it back to the client to execute. Finally, you invoke Bridge.close() to close the execution.

 1 Bridge bridge = Bridge.start(svlctx, request, response, desktop);
 2 try {
 3     //execution started, do whatever you want
 4 
 5     String jscode = bridge.getResult();
 6 
 7     //send jscode back with the foreign Ajax channel.
 8 } finally {
 9     bridge.close(); //end of execution and cleanup
10 }

Example

Start Execution in JSF 2 ActionListener

In JSF 2.0 developers can initiate Ajax request using jsf.ajax.request [1] For e.g.

1 ...
2    <h:commandButton id="save" value="Save"
3     onclick="jsf.ajax.request(this, event, {execute:'@all'}); return false;" actionListener="${myBean.saveDetails}">
4    </h:commandButton>
5 ...

and in your ActionListener

 1 @ManagedBean
 2 @SessionScoped
 3 public class MyBean {
 4 
 5    public void saveDetails(ActionEvent e)  throws IOException {
 6 
 7       ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
 8       ServletContext svlctx = (ServletContext) ec.getContext();
 9       HttpServletRequest request = (HttpServletRequest) ec.getRequest();
10       HttpServletResponse response = (HttpServletResponse) ec.getResponse();
11       Component comp = getComponent();
12       Bridge bridge = Bridge.start(svlctx, request, response,comp.getDesktop());
13       try {
14               // update ZK component(s) state here
15               //comp.appendChild(new SomethingElse()); ...
16 
17               //Send back bridge.getResult() with the response writer (eval)
18               PartialResponseWriter responseWriter =
19                 FacesContext.getCurrentInstance().getPartialViewContext().getPartialResponseWriter();
20               responseWriter.startDocument();
21               responseWriter.startEval();
22               responseWriter.write(bridge.getResult());
23               responseWriter.endEval();
24               responseWriter.endDocument();
25               responseWriter.flush();
26               responseWriter.close();
27       } finally {
28               bridge.close();
29       }
30   }
31 
32   private Component getComponent() {
33       //locate the component that you want to handle
34   }
35 }

  1. For more information on jsf.ajax.request read official JSF Javascript docs for jsf.ajax.

Version History

Version Date Content
5.0.5 September 2010 Bridge was introduced to simplify the starting of an execution in foreign Ajax channel



Last Update : 2022/07/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.