Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Warning
titleDojo plugin is deprecated

The Dojo plugin will be deprecated on Struts 2.1

...

Note
titleRemember

To use these ajax tags you need to set the "theme" attribute to "ajax".

Common Attributes

These attributes are common to all the ajax tags:

Attribute

Description

Example

href

url used to make the request

/AjaxTest.action

beforeLoading

Javascript code which will be executed before the request is made

alert('before!');

afterLoading

Javascript code which will be executed after the request is made

alert('after!');

Div

The div tag is a content area that can load its content asynchronously. The div tag can be forced to reload its content using a topic|http://dojo.jot.com/WikiHome/EventExamples]. To defined the topic that will trigger the refresh of the panel, use the refreshListenTopic" attribute.

This div will refresh every time the topic "/refresh" is published:

...


<s:div theme="ajax" href="/AjaxTest.action" refreshListenTopic="/refresh"/>

To publish the topic of this example use:

...


dojo.event.topic.publish("/refresh");

The div tag can be configured to update its content periodically, using a timer. Use the "updateInterval" attribute to set the interval used to refresh the content, this value is expressed in milliseconds. If "autoStart" is set to true, "delay" can be used to force the timer to wait for the delay period before starting. Delay is expressed in milliseconds.

This div will refresh periodically every 2 seconds, starting 3 seconds after the page is loaded:

...


<s:div theme="ajax" href="/AjaxTest.action" updateInterval="2000" delay="3000"/>

There is an "autoStart" attribute that controls whether the timer will be started when the page is loaded. This attribute is "true" by default. The timer can be started and stoped using topics through the attributes "startTimerListenTopic" and "stopTimerListenTopic".

This div will not start the timer by default and will listen to the start/stop topics to start its timer:

...


<s:div theme="ajax" href="/AjaxTest.action" startTimerListenTopic="/startTimer" stopTimerListenTopic="/stopTimer" updateInterval="3000" autoStart="false"/>

The div panel shows "Loading..." by default when the request is in process. To customize this text, use the "loadingText" attribute. If an error of any sort occurs, the error will be shown in the div area, to customize the error message, use the "errorText" attribute.

This div uses custom error/loading messages:

...


<s:div href="/AjaxTest.action" theme="ajax" errorText="There was an error" loadingText="reloading" updateInterval="5000"/>

If the loaded text contains javascript code sections, these sections will be evaluated by the div tag if the "executeScripts" attribute is set to true.

If parameters need to be passed to the url, the "formId" attribute can be used to specify a form whose fields will be serialized and passed on the request as parameters. The attribute "formFilter" can bet set to the name of a javascript function that will be used to filter the fields of "formId".

This div will submit the field "firstName", of the form "userData" and ignore other fields:

...


<script type="text/javascript">
  function filter(field) {
    return field.name == "firstName";
  }
</script>

<form id="userData">
	<label for="firstName">First Name</label>
	<input type="textbox" id="firstName" name="firstName">
        <label for="lastName">Last Name</label>
	<input type="textbox" id="lastName" name="lastName">
</form>
<s:div href="/AjaxTest.action" theme="ajax" formId="userData" formFilter="filter"/>
Struts 2.0 versus Struts 2.1 and the Dojo tags

The easiest way to get documentation for Struts 2.0 Dojo tag usage is to look at older Struts 2 documentation, like the Struts 2.0.11 Ajax tags wiki documentation.

Please check that documentation and the Dojo tag examples in the showcase app of the appropriate Struts 2 version before asking questions on the struts-user mailing list!


THE WIKI IS NOT VERSIONABLE (in a practical way).

The documentation here is for the most current Struts 2, not necessarily the most current release. We try to add version-specific documentation notes but have undoubtedly missed some locations.

Description

To use the AJAX tags from 2.1 on you must:

  • Include the Dojo Plugin distributed with Struts 2 in your /WEB-INF/lib folder.
  • Add <%@ taglib prefix="sx" uri="/struts-dojo-tags" %> to your page.
  • Include the head tag on the page, which can be configured for performance or debugging purposes.

Handling AJAX Responses

The following attributes affect the handling of all ajax responses.

Attribute

Default Value

Description

parseContent

true

When true, Dojo will parse the response into an XHTML Document Object and traverse the nodes searching for Dojo Widget markup. The parse and traversal is performed prior to inserting the nodes into the DOM. This attribute must be enabled to nest Dojo widgets (dojo tags) within responses. There's significant processing involved to create and parse the document so switch this feature off when not required. Note also that the response must be valid XHTML for cross-browser support and widgets must have unique IDs.

separateScripts

true

When true, Dojo will extract the <script> tags from the response, concatenate the extracted code into one block, create a new Function whose body is the extracted code and immediately invoke the function. The invocation is performed after the DOM has been updated with the XHTML. The function is executed within the scope of the widget (that is, the this variable points to the widget instance).
When false, Dojo will extract the <script> tags from the response, concatenate the extracted code into one block and:
*in IE: invoke window.execScript() on the code
*in other browsers: create a <script> node containing the code and insert that node into the DOM
This invocation occurs after the DOM has been updated with the XHTML. Note that scripts may not be executed if it is not valid to create a <script> node in the DOM at the destination.

executeScripts

false

When true, Dojo will extract code from the <script> tags from the response and execute it based on the separateScripts value.
When false, the XHTML response is inserted into the DOM and <script> nodes are ignored.

Note

It's possible that the updated DOM will not include <script> nodes even though the inline code has been executed.

Tip

Ensure the response is XHTML-compliant (including, for example, thead and tbody tags in tables).
If you intend to run inline javascript:
*Ensure the javascript can be concatenated and executed in one block,
*set executeScripts=true,
*set separateScripts=true (the reliable option)

Topics

Most of the AJAX tags use Dojo topics for event notification and communication between them, to learn about topics visit Dojo's documentation

Examples

Examples can be found on the documentation for each tag in the UI Tag Reference page, for additional examples see Ajax and JavaScript Recipes and the Showcase application distributed with Struts 2.

Tags

If the attribute "handler" is set, the javascript function specified by its value will be called, instead of making the request.

This div will not make any request, and will just call the function "handler". The first parameter of the function is the Dojo widget for the div, and the second the DOM node for the div.

...