Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
This documentation refers to version 2.1 which has not been released yet.
Warning
titleDojo plugin is deprecated

The Dojo plugin will be deprecated on Struts 2.1

2.1

Table of Contents
minLevel3
indent20px
stylenone

Common

All examples on this page assume the following JSP fragment is on the same page as the example.

Code Block
HTML
HTML
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<head>
    <sx:head />
</head>

<s:url id="url" value="/MyAction.action" />

Requests

Request is triggered by a topic

...

Code Block
HTML
HTML
<!-- With a submit tag -->
<s:form namespace="/mynamespace" action="MyAction">
  <input type=textbox name="data">
  <sx:submit />
</s:form>

<!-- With an anchor tag -->
<s:form namespace="/mynamespace" action="MyAction">
  <input type=textbox name="data">
  <sx:a />
</s:form>

Div

Loads its content after page is loaded

...

Code Block
HTML
HTML
<sx:div href="%{#url}"
        listenTopics="/refresh"
	startTimerListenTopics="/startTimer"
	stopTimerListenTopics="/stopTimer"
	updateFreq="3000">
  Initial Content
</sx:div>

<s:submit value="Refresh" onclick="dojo.event.topic.publish('/refresh')" />
<s:submit value="Start refresh timer" onclick="dojo.event.topic.publish('/startTimer')" />
<s:submit value="Stop refresh timer" onclick="dojo.event.topic.publish('/stopTimer')" />

Date and Time picker

Date picker
Code Block
HTML
HTML
<sx:datetimepicker name="picker" />

...

Code Block
HTML
HTML
<script type="text/javascript">
  dojo.event.topic.subscribe("/value", function(textEnteredtext, date, widget){
      alert('value changed');
      //textEntered: String eneteredentered in the textbox
      //date: JavaScript Date object with the value selected
      //widetwidget: widget that published the topic 
  });
</script> 

<sx:datetimepicker label="Order Date" valueNotifyTopics="/value"/>
Use other locales.
Warning

Locales must be specified in the

...

sx:

...

head tag.

Code Block
HTML
HTML
<sx:head extraLocales="en-us,nl-nl,de-de" />

<sx:datetimepicker label="In German" name="dddp7" value="%{'2006-06-28'}" language="de-de" />
<sx:datetimepicker label="In Dutch"  name="dddp8" value="%{'2006-06-28'}" language="nl-nl" />

Tabbed Panel

Local Tabs
Code Block
HTML
HTML
<sx:tabbedpanel>tabbedpanel id="tabContainer">
   <sx:div label="Tab 1" >
       Local Tab 1
   </sx:div>   
   <sx:div label="Tab 2" >
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>
Local and remote tabs
Code Block
HTML
HTML
<sx:tabbedpanel>tabbedpanel id="tabContainer">
   <sx:div label="Local Tab 1" >
       Tab 1
   </sx:div>   
   <sx:div label="Remote Tab 2" href="%{#url}">
       Remote Tab 2
   </sx:div>   
</sx:tabbedpanel>

...

Code Block
HTML
HTML
<sx:tabbedpanel cssStyle="width: 500px; height: 300px;" doLayout="true" id="tabContainer">
   <sx:div label="Tab 1" >
       Local Tab 1
   </sx:div>   
   <sx:div label="Tab 2" >
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>
Do not load tab 2 when page loads (it will be loaded when selected)
Code Block
HTML
HTML
<sx:tabbedpanel>
   tabbedpanel id="tabContainer">
   <sx:div label="Remote Tab 1" href="%{#url}">
       Remote Tab 1
   </sx:div>  
   <sx:div label="Remote Tab 2" href="%{#url}" preload="false">
       Remote Tab 1
   </sx:div>      
</sx:tabbedpanel>
Reload tabs content when selected
Code Block
HTML
HTML
<sx:tabbedpanel>tabbedpanel id="tabContainer">
   <sx:div label="Remote Tab 1" href="%{#url}" refreshOnShow="true">
       Remote Tab 1
   </sx:div>  
   <sx:div label="Remote Tab 2" href="%{#url}" refreshOnShow="true">
       Remote Tab 2
   </sx:div>      
</sx:tabbedpanel>
Disabled tabs
Code Block
HTML
HTML
<sx:tabbedpanel>tabbedpanel id="tabContainer">
   <sx:div label="Tab 1" >
       Local Tab 1
   </sx:div>   
   <sx:div label="Tab 2" disabled="true">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>

...

Code Block
HTML
HTML
<script type="text/javascript">
   function enableTab(idparam) {
      var tabContainer = dojo.widget.byId('tabContainer');
      tabContainer.enableTab(idparam);
   }
    
   
   function disableTab(indexparam) {
      var tabContainer = dojo.widget.byId('tabContainer');
      tabContainer.disableTab(indexparam);
   }
</script>

<sx:tabbedpanel id="tabContainer" id="tabContainer">
   <sx:div id="tab1" label="Tab 1" >
       Local Tab 1
   </sx:div>   
   <sx:div id="tab2" label="Tab 2" disabled="true">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>

<!-- By Tab Index -->
<input type="button" onclick="enableTab(1)" value="Enable Tab 2 using Index" />
<input type="button" onclick="disableTab(1)" value="Disable Tab 2 using Index" />
    
<!-- By Tab Id -->
<input type="button" onclick="enableTab('tab2')" value="Enable Tab 2 using Id" />
<input type="button" onclick="disableTab('tab2')" value="Disable Tab 2 using Id" />
   
<!-- By Widget -->
<input type="button" onclick="enableTab(dojo.widget.byId('tab2'))" value="Enable Tab 2 using widget" />
<input type="button" onclick="disableTab(dojo.widget.byId('tab2'))" value="Disable Tab 2 using widget" />
Set Tab labels position to bottom (can be: top, right, bottom, left)
Code Block
HTML
HTML
<sx:tabbedpanel labelposition="bottom" id="tabContainer">
   <sx:div label="Tab 1" >
       Local Tab 1
   </sx:div>   
   <sx:div label="Tab 2" >
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>
Allow tab 2 to be removed(closed)
Code Block
HTML
HTML
<sx:tabbedpanel>tabbedpanel id="tabContainer">
   <sx:div label="Tab 1" >
       Local Tab 1
   </sx:div>   
   <sx:div label="Tab 2"  closable="true">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>

...

Code Block
HTML
HTML
<script type="text/javascript">
   dojo.event.topic.subscribe('/before', function(event, tab, tabContainer) {
      alert("Before selecting tab");
   });

   dojo.event.topic.subscribe('/after', function(tab, tabContainer) {
      alert("After tab was selected");
   });
</script>
<sx:tabbedpanel beforeSelectTabNotifyTopics="/before" afterSelectTabNotifyTopics="/after" id="tabContainer">
   <sx:div label="Tab 1">
       Local Tab 1
   </sx:div>   
   <sx:div label="Tab 2">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>

...

Select tab using JavaScript
Code Block
HTML
HTML
<script type="text/javascript">
    dojo.event.topic.subscribe('/before', function(event, tab, tabContainerfunction selectTab(id) {
     var if(tab.widgetIdtabContainer == "tab1dojo.widget.byId("tabContainer") {;
         event.cancel = true;
      }tabContainer.selectTab(id);
   });
</script>
<sx:tabbedpanel beforeSelectTabNotifyTopicsid="/beforetabContainer">
   <sx:div idlabel="tab1Tab 1" labelid="Tab 1tab1">
       Local Tab 1
   </sx:div>   
   <sx:div id="tab2" label="Tab 2">
  id="tab2">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>
Customize template css path (Dojo widget template css)

<input type="button" onclick="selectTab('tab1')" value="Select tab 1" />
<input type="button" onclick="selectTab('tab2')" value="Select tab 2" />
Prevent tab 2 from being selected
Code Block
HTML
HTML

<script type="text/javascript">
   dojo.event.topic.subscribe('/before', function(event, tab, tabContainer) {
      event.cancel = tab.widgetId == "tab2" ;
   });
</script>
<sx:tabbedpanel beforeSelectTabNotifyTopics="/before" id="tabContainer">
   <sx:div id="tab1" label="Tab 1">
       Local Tab 1
Code Block
HTMLHTML

<sx:tabbedpanel templateCssPath="%{#cssUrl}" >
   <sx:div id="tab1" label="Tab 1">
       Local Tab 1
   </sx:div>   
   <sx:div id="tab2" label="Tab 2">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>   <sx:div id="tab2" label="Tab 2">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>
Customize template css path (Dojo widget template css)
Code Block
HTML
HTML

<sx:tabbedpanel templateCssPath="%{#cssUrl}" id="tabContainer">
   <sx:div id="tab1" label="Tab 1">
       Local Tab 1
   </sx:div>   
   <sx:div id="tab2" label="Tab 2">
       Local Tab 2
   </sx:div>   
</sx:tabbedpanel>

Autocompleter

Fixed list
Code Block
HTML
HTML

<sx:autocompleter list="{'apple','banana','grape','pear'}" />
Set initial value
Code Block
HTML
HTML

<sx:autocompleter list="{'apple','banana','grape','pear'}" value="apple"/>
Force valid option (restore option when focus is lost)
Code Block
HTML
HTML

<sx:autocompleter list="{'apple','banana','grape','pear'}" forceValidOption="true"/>
Using the JSON plugin to generate the values (one of the possible ways)

The action

Code Block
Java
Java
titleAutocompleterExample.java

public class AutocompleterExample extends ActionSupport {

   public Map<String, String> getOptions() {
      Map<String,String> options = new HashMap<String,String>();
      options.put("Florida", "FL");
      options.put("Alabama", "AL");

      return options;
   }
}

The mapping:

Code Block
XML
XML
titlestruts.xml

<struts>
...
   <package name="autocompleter" namespace="/autocompleter" extends="json-default">
       <action name="getStates" class="AutocompleterExample">
           <result type="json">
               <param name="root">options</param></result>
       </action>
   </package>
...
</struts>

The JSP (fragment):

Code Block
HTML
HTML
1JSP page

<s:url id="optionsUrl" namespace="/autocompleter" action="getStates" />

<sx:autocompleter href="%{#optionsUrl}" />
Example action

When a form containing an autocompleter is submitted, two values will be submitted for each autocompleter, one for the selected value, and one for its associated key.

The action:

Code Block
java
java
titleMyAction.java

public MyAction extends ActionSupport {
    private String optionsKey;
    private String options;

    ...    
}

The JSP:

Code Block
HTML
HTML

<s:form id="form">
  <sx:autocompleter name="options" label="Options" />
</s:form>
Set initial key and value
Code Block
HTML
HTML
1JSP page

<s:url id="optionsUrl" namespace="/autocompleter" action="getStates" />

<sx:autocompleter href="%{#optionsUrl}" value="Florida" keyValue="FL"/>
Change default key name

The action:

Code Block
java
java
titleMyAction.java

public MyAction extends ActionSupport {
    private String superKey;
    private String options;

    ...    
}

The JSP:

Code Block
HTML
HTML

<s:form id="form">
  <sx:autocompleter keyName="superKey" name="options" label="Options" />
</s:form>
JSON accepted

for this autocompleter:

Code Block
HTML
HTML

<sx:autocompleter name="state" />

The following JSON will be accepted:

Code Block
javascript
javascript
titleMap(recommended as it is the easiest one to generate)

{
    "Alabama" : "AL",
    "Alaska" : "AK"
}
Code Block
javascript
javascript
titleArray of arrays

[
    ["Alabama", "AL"],
    ["Alaska", "AK"]
]
Code Block
javascript
javascript
titleArray inside object, same name as field

{
    "state" : [
        ["Alabama","AL"],
        ["Alaska","AK"]
    ]
}     
Code Block
javascript
javascript
titleMap inside object, same name as field

{
    "state" : {
        "Alabama" : "Alabama",
        "Alaska" : "AK"
    }
}    
Code Block
javascript
javascript
titleArray inside object, field in response starts with the name of the autocompleter("state" in this example)

{
    "states" : [
        ["Alabama","AL"],
        ["Alaska","AK"]
    ]
}     
Code Block
javascript
javascript
titleNo name match, use first array found, and hope for the best

{
    "Australopithecus" : [
       ["Alabama","AL"],
       ["Alaska","AK"]
    ]
}     
Load characters while user types (when text size >= 3)
Code Block
HTML
HTML

<sx:autocompleter href="%{#url}" loadOnTextChange="true" loadMinimumCount="3" />
Hide dropdown arrow
Code Block
HTML
HTML

<sx:autocompleter href="%{#url}" showDownArrow="false" />
Limit options shown to 3
Code Block
HTML
HTML

<sx:autocompleter href="%{#url}" resultsLimit="3" />
All matching options are shown
Code Block
HTML
HTML

<sx:autocompleter href="%{#url}" resultsLimit="-1" />
Set dropdown height and width, in pixels
Code Block
HTML
HTML

<sx:autocompleter href="%{#url}" dropdownHeight="180" dropdownWidth="200" />
Disable it when page is loaded
Code Block
HTML
HTML

<sx:autocompleter href="%{#url}" disabled="true" />
Disable it/enable it using JavaScript
Code Block
HTML
HTML

<script type="text/javascript">
  function enableit() {
     var autoCompleter = dojo.widget.byId("auto");
     autoCompleter.enable();
  }

  function disableit() {
     var autoCompleter = dojo.widget.byId("auto");
     autoCompleter.disable();
  }
</script>

<sx:autocompleter id="auto" href="%{#url}" />
Reload options when topic is published
Code Block
HTML
HTML

<sx:autocompleter listenTopics="/reload" href="%{#url}" />
Submit form when options are loaded
Code Block
HTML
HTML

<s:form id="form">
  <input type="textbox" name="data">
</s:form>

<sx:autocompleter formId="form" href="%{#url}"  />
Filter fields top be submitted when options are loaded (return true to include)
Code Block
HTML
HTML

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

<s:form id="form">
  <input type="textbox" name="data0">
  <input type="textbox" name="data1">
</s:form>

<sx:autocompleter formId="form" formFilter="filter" href="%{#url}" />
Link two autocompleters, using topics
Code Block
HTML
HTML

<form id="selectForm">
   <sx:autocompleter  name="select" list="{'fruits','colors'}"  valueNotifyTopics="/changed" />
</form>

<sx:autocompleter  href="%{#url}" formId="selectForm" listenTopics="/changed" />
Show options, but don't make suggestion (autocomple) in the textbox
Code Block
HTML
HTML

<sx:autocompleter autoComplete="false" href="%{#url}" />
Prevent options from loading when page loads
Code Block
HTML
HTML

<sx:autocompleter preload="false" href="%{#url}" />