You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Struts 2 provides a set of tags to help you ajaxify your applications, most of them inherited from Webwork, and based on Dojo. Dojo 0.4 is included in Struts 2.

Remember

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. 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"/>

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.

<script type="text/javascript">
   function handler(widget, node) {
     alert('I will handle this myself!');
	 node.innerHTML = "Done";
   }
</script>

<s:div theme="ajax" href="/AjaxTest.action" handler="handler"/>
  • No labels