Example 3: Using Multiple Actions
Writing Taconite responses are really easy. As shown in Examples 1 and 2, all you need to do is embed your content in special Taconite tags, provide the context node's ID attribute value, and Taconite does the rest. But, you may bet thinking to yourself: Wouldn't it be even easier if the taconite root tag wasn't there? If it were removed the remaining action tag would still constitute a valid XML document, as it would be the root. Isn't the taconite root tag just fluff?
The taconite root tag, put simply, allows you to use multiple actions in a single response. When might this be useful?
Consider an Ajax-powered application where the user enters a new employee's name, title, and salary in fields on the Web page. Upon clicking the "Save" button, the name, title, and salary are sent to the server as an Ajax request, and the server saves the new employee's data. The server responses by adding the new user as a row to the table that lists all the company's employees, using the append as children action.
So far so good. But as part of this response, you also want to set the message on the bottom of the page to "Employee Smith, ID #0000, successfully added" using the replace children action. OK, but I already used on action on the response -- the one that adds the new employee to the employee list table. What should one do?
You can now see why the taconite root tag is so important. With it, you can use as many actions as you'd like in the response, and the response is still valid XML. Without the taconite root tag, you would only be able to use one action per response.
The XHTML Page
The client XHTML page is a non-sensical page that has two links and one piece of text that says "I was here first!". To demonstrate how two actions can be used in one response, clicking either link on the page causes one new line to be inserted before the "I was here first" text and one new line to be inserted after the "I was here text."
Other than that this page is very similar to the XHTML page in Example 1. No parameters are being sent to the server and there is no other fancy stuff happening, so we'll save you the trouble of having to sit through another explanation of wha's happening. The source of the XHTML page is listed below.
01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 02 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 03 04 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 05 <head> 06 <title>Taconite Example</title> 07 <script type="text/javascript" src="/taconite-examples/js/taconite-client.js"></script> 08 <script type="text/javascript" src="/taconite-examples/js/taconite-parser.js"></script> 09 10 <script type="text/javascript"> 11 function addContent(url) { 12 var ajaxRequest = new AjaxRequest(url); //Create AjaxRequest object 13 ajaxRequest.sendRequest(); //Send the request 14 } 15 </script> 16 </head> 17 <body> 18 <br/> 19 <br/> 20 <a href="javascript:addContent('example3-all.jsp');">Add Content</a> 21 22 <div> 23 <div id="firstHere" style="font-weight:bold;color:red;">I was here first!!</div> 24 </div> 25 26 </body> 27 </html>
Writing the Server Response
The server response uses two actions: one that inserts text before the "I was here first" message, and one that inserts text after the "I was here first" message.
The taconite-insert-before and taconite-insert-after tags, respectively, accomplish these tags. These actions do exactly what they say they do. The taconite-insert-before action inserts its content immediately before the context node. the taconite-insert-after action inserts its content immediately after the context node. Following is the code listing for the JSP that uses the "hand written" Taconite tags.
01 <%@page contentType="text/xml"%> 02 03 <taconite-root xml:space="preserve"> 04 05 <taconite-insert-before contextNodeID="firstHere"> 06 07 <div style="font-weight:bold;color:orange;"> 08 I am inserted before! 09 </div> 10 11 </taconite-insert-before> 12 13 <taconite-insert-after contextNodeID="firstHere"> 14 15 <div style="font-weight:bold;color:green;"> 16 I am inserted after! 17 </div> 18 19 </taconite-insert-after> 20 21 </taconite-root>
Note how there are two action tags within the taconite-root tag. It doesn't matter what order they're in, although they are evaluated in the order in which they appear here, so if you want the actions to be evaluated in a certain order, use that order here.
The taconite-insert-before tag inserts the text "I am inserted before" in front of the "I was here first" message. Note that the ID of the div containing the "I was here first" message is the same as the contextNodeID. Similarly, the taconite-insert-after tag inserts the "I am inserted after" text immediately after the "I was here first" message.
Summary
Taconite is very flexible in that it allows you to use multiple actions within the same response. The insert before and insert after actions do just what they say: insert content immediately before the context node and insert content immediately after the context node.