User-defined actions for the web components

You can implement user-defined actions for some of the web components.

ilog.webui.dhtml.components provides an action mechanism that allows you to implement user-defined actions. For example, you can use the method setAction to provide an action to be executed just after the user selects a menu item. An action is always executed on the client side by the web browser. Actions can be executed on the server or on the client, depending on the type of action.

JavaScript actions

Class: IlxWJSAction

You can use JavaScript actions to call user-defined JavaScript functions on the client side. To create a JavaScript action, pass the name of your JavaScript function to the IlxWJSAction constructor. When the action is executed on the web browser, for example when the user selects a menu item, the JavaScript function is called. The arguments passed to the function depend on the component with which the action is associated. (See the Java™ API Reference documentation for details.)

Request actions

Class: IlxWRequestAction

A request action is an action that contains a request descriptor object.

To understand the purpose of request actions, suppose that you want to add a link from the current HTML page to another HTML page. The standard way to do so is to add a direct hypertext link in the usual format: <a href="..."></a>. However, if the source page has been edited on the HTML client but the web server has not been updated accordingly (for example, a text field has been filled in), some data might be lost when the link is activated. To avoid this problem, you must send the request through the controller servlet on the web server, rather than sending it directly to the web server.

The component deployer creates an instance of the class ilog.webui.dhtml.components.IlxWRequestAction. This IlxWRequestAction instance contains the descriptor of the request itself, an instance of the class IlxWRequestDesc.

When the client executes the request action, the request descriptor and the modified-component queue are sent to the controller servlet. The controller servlet processes the modified-component queue and redirects the request to the target path specified in the descriptor. This mechanism makes sure that, when the request is processed, the state of the components contained in the current manager on the server is consistent with the JavaScript proxy.

Request Action diagram

Java actions

Class: IlxWJavaAction

A Java action is a request action that includes a method perform. In the figure above, the perform method is executed in step 2.

You can use Java actions to call user-defined Java functions on the server side. This method is executed on the server, just before forwarding the call specified in the request descriptor. As in JavaScript actions, the arguments passed to the perform method depend on the component with which the action is associated. (See the Java API Reference Manual for details.)

Action holders

You can implement user-defined actions using action holders. An action holder is a hidden web component. Its only purpose is to hold an action that the component deployer can execute programmatically, but that has no corresponding graphical representation in the client.

Action holders are instances of the class IlxWActionHolder. The method getJSPerformCall generates JavaScript code that is executed on the client and executes the action associated with the action holder.

The following example shows how action holders can be useful. You want to add a link to the page generated by MyServlet, and you want the link to save the current rule displayed by the Rule Editor.

Respectively, the two following pieces of code show how to add a direct link to myServlet, and to call an action holder action. The action holder action is a more efficient solution than the myServlet link.

public MyServlet extends HttpServlet {
  . . .

  public void doPost(HttpServletRequest request,
                     HttpservletResponse response) {
    . . . 
    out.println("<html>");
    out.println("<body>");
    if ("true".equals(request.getParameter("save"))) {
      editor.getTokenModel().write(new FileWriter(".brl"));
      out.println("Rule saved.");
    } else {
      port.open();
      out.println("<h1>This is the editor</h1>");
      editor.print(port);
      // The wrong way: add a direct link to MyServlet
      out.println("<a href=\"/myapp/servlet/MyServlet?save=true\">");
      out.println("Save me</a>");      port.close();
    }
    out.println("</body>");
    out.println("</html>");
  }

}

Do not call the servlet MyServlet directly using a parameter for saving the rule on the server side. The web browser might not be synchronized with the server because the data in the modified-component queue has not launched an action to synchronize with the server. This problem can occur when you type in the text field of a rule in the Web Rule Editor. The modified-component queue is updated, but it is not sent immediately to the server, so a direct call to the servlet might save a rule that has not been synchronized with the client view.

You can use an action holder to save a rule and make sure that the modified-component queue has been sent to the server. To use an action holder, rewrite MyServlet as follows:

public MyServlet extends HttpServlet {
  . . .

  public void doPost(HttpServletRequest request,
                     HttpservletResponse response) {
    . . . 
    // Create an action holder.
    IlxWActionHolder ah = manager.getComponentNamed("ah");
    if (ah==null) {
      IlxWRequestDesc desc =
         IlxWRequestDesc.get("/servlet/MyServlet?save=true", _self");
      ah = new IlxWActionHolder(new IlxWRequestAction(desc));
      ah.setName("ah");
      manager.add(ah);
    }
    if ("true".equals(request.getParameter("save"))) {
      editor.getTokenModel().save(new FileWriter("myrule.brl"));
      out.println("Rule saved.");
    } else {
      port.open();
      out.println("<h1>This is the editor</h1>");
      editor.print(port);
      ah.print(port);
      // The right way: call the action of the action holder.      out.println("<a href=\"javascript:"+ah.getJSPerformCall()+"\">");
      out.println("Save me</a>");
      port.close();
    }
    out.println("</body>");
    out.println("</html>");
  }

}