...
Using the JSON plugin to generate the values (one of the possible ways)
The action
Code Block |
---|
| Java |
---|
| Java |
---|
title | AutocompleterExample.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 |
---|
|
<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 |
---|
|
<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 |
---|
title | MyAction.javajava |
---|
|
public MyAction extends ActionSupport {
private String optionsKey;
private String options;
...
}
|
...
Set initial key and value
Code Block |
---|
|
<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 |
---|
title | MyAction.javajava |
---|
|
public MyAction extends ActionSupport {
private String superKey;
private String options;
...
}
|
...
The following JSON will be accepted:
Code Block |
---|
| javascript |
---|
| javascript |
---|
title | Map(recommended as it is the easiest one to generate)javascript |
---|
|
{
"Alabama" : "AL",
"Alaska" : "AK"
}
|
Code Block |
---|
| javascript |
---|
| javascript |
---|
title | Array of arraysjavascript |
---|
|
[
["Alabama", "AL"],
["Alaska", "AK"]
]
|
Code Block |
---|
| javascript |
---|
| javascript |
---|
title | Array inside object, same name as fieldjavascript |
---|
|
{
"state" : [
["Alabama","AL"],
["Alaska","AK"]
]
}
|
Code Block |
---|
| javascript |
---|
| javascript |
---|
title | Map inside object, same name as fieldjavascript |
---|
|
{
"state" : {
"Alabama" : "Alabama",
"Alaska" : "AK"
}
}
|
Code Block |
---|
| javascript |
---|
| javascript |
---|
title | Array 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 |
---|
title | No name match, use first array found, and hope for the bestjavascript |
---|
|
{
"Australopithecus" : [
["Alabama","AL"],
["Alaska","AK"]
]
}
|
...