Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Using the JSON plugin to generate the values (one of the possible ways)

The action

Code Block
Java
Java
titleAutocompleterExample.javaJava
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.xmlXML
<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 pageHTML
<s:url id="optionsUrl" namespace="/autocompleter" action="getStates" />

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

...

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.javajava
public MyAction extends ActionSupport {
    private String optionsKey;
    private String options;

    ...    
}

...

Set initial key and value
Code Block
HTML
HTML
1JSP pageHTML
<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.javajava
public MyAction extends ActionSupport {
    private String superKey;
    private String options;

    ...    
}

...

The following JSON will be accepted:

Code Block
javascript
javascript
titleMap(recommended as it is the easiest one to generate)javascript
{
    "Alabama" : "AL",
    "Alaska" : "AK"
}
Code Block
javascript
javascript
titleArray of arraysjavascript
[
    ["Alabama", "AL"],
    ["Alaska", "AK"]
]
Code Block
javascript
javascript
titleArray inside object, same name as fieldjavascript
{
    "state" : [
        ["Alabama","AL"],
        ["Alaska","AK"]
    ]
}     
Code Block
javascript
javascript
titleMap inside object, same name as fieldjavascript
{
    "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)javascript
{
    "states" : [
        ["Alabama","AL"],
        ["Alaska","AK"]
    ]
}     
Code Block
javascript
javascript
titleNo name match, use first array found, and hope for the bestjavascript
{
    "Australopithecus" : [
       ["Alabama","AL"],
       ["Alaska","AK"]
    ]
}     

...