Start Execution in Foreign Ajax Channel"

From Documentation
m (use syntaxhighlight (via JWB))
Line 8: Line 8:
 
Starting an execution in a foreign Ajax channel is straightforward: invoke <javadoc method="start(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.zkoss.zk.ui.Desktop)">org.zkoss.zkplus.embed.Bridge</javadoc>. Then, you are allowed to access the components, post events and do anything you like. At the end, you invoke <javadoc method="getResult()">org.zkoss.zkplus.embed.Bridge</javadoc> to retrieve the JavaScript code snippet and send it back to the client to execute. Finally, you invoke <javadoc method="close()">org.zkoss.zkplus.embed.Bridge</javadoc> to close the execution.
 
Starting an execution in a foreign Ajax channel is straightforward: invoke <javadoc method="start(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.zkoss.zk.ui.Desktop)">org.zkoss.zkplus.embed.Bridge</javadoc>. Then, you are allowed to access the components, post events and do anything you like. At the end, you invoke <javadoc method="getResult()">org.zkoss.zkplus.embed.Bridge</javadoc> to retrieve the JavaScript code snippet and send it back to the client to execute. Finally, you invoke <javadoc method="close()">org.zkoss.zkplus.embed.Bridge</javadoc> to close the execution.
  
<syntax lang="java">
+
<syntaxhighlight line lang="java">
 
Bridge bridge = Bridge.start(svlctx, request, response, desktop);
 
Bridge bridge = Bridge.start(svlctx, request, response, desktop);
 
try {
 
try {
Line 19: Line 19:
 
     bridge.close(); //end of execution and cleanup
 
     bridge.close(); //end of execution and cleanup
 
}
 
}
</syntax>
+
</syntaxhighlight>
  
 
= Example =
 
= Example =
Line 25: Line 25:
 
== Start Execution in JSF 2 ActionListener ==
 
== Start Execution in JSF 2 ActionListener ==
 
In JSF 2.0 developers can initiate Ajax request using jsf.ajax.request <ref>For more information on [https://javaserverfaces.dev.java.net/nonav/docs/2.0/jsdocs/symbols/jsf.ajax.html#.request jsf.ajax.request] read official JSF Javascript docs for [https://javaserverfaces.dev.java.net/nonav/docs/2.0/jsdocs/symbols/jsf.ajax.html jsf.ajax].</ref> For e.g.
 
In JSF 2.0 developers can initiate Ajax request using jsf.ajax.request <ref>For more information on [https://javaserverfaces.dev.java.net/nonav/docs/2.0/jsdocs/symbols/jsf.ajax.html#.request jsf.ajax.request] read official JSF Javascript docs for [https://javaserverfaces.dev.java.net/nonav/docs/2.0/jsdocs/symbols/jsf.ajax.html jsf.ajax].</ref> For e.g.
<syntax lang="xml">
+
<syntaxhighlight line lang="xml">
 
...
 
...
 
   <h:commandButton id="save" value="Save"
 
   <h:commandButton id="save" value="Save"
Line 31: Line 31:
 
   </h:commandButton>
 
   </h:commandButton>
 
...
 
...
</syntax>
+
</syntaxhighlight>
  
 
and in your ActionListener
 
and in your ActionListener
  
<syntax lang="java" high="12,22,28">
+
<syntaxhighlight line lang="java" high="12,22,28">
 
@ManagedBean
 
@ManagedBean
 
@SessionScoped
 
@SessionScoped
Line 71: Line 71:
 
   }
 
   }
 
}
 
}
</syntax>
+
</syntaxhighlight>
  
 
<blockquote>
 
<blockquote>

Revision as of 09:04, 24 June 2021


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

Last Update : 2021/06/24


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



Last Update : 2021/06/24

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