By mkyong Download It – Struts2-AutoCompleter-JSON-Example.zip In last Struts 2 autocompleter example, you learn about how to generate a list of the select options to the autocompleter component via Java list and ONGL expression. Alternatively, it’s possible to generate the select options via JSON data as well. Before you proceed, make sure you understand the basic usage of autocompleter component and JSON plugin. Read the below articles. Struts 2 autocompleter + JSON exampleIn this tutorials, you will use Struts 2 JSON plugin to convert an object into JSON format, and pass it to the autocompleter component. 1. Get dependency libraryGet the all the dependency libraries. pom.xml <!-- Struts 2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> <!-- Struts 2 Dojo Ajax Tags --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-dojo-plugin</artifactId> <version>2.1.8</version> </dependency> <!-- Struts 2 JSON Plugins --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.1.8</version> </dependency> 2. ActionA class to convert into JSON format later, to provide a list of the select options to the autocompleter component. DatabaseJSON.java package com.mon.action; import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.Action; public class DatabaseJSON{ private Map<String, String> databases = new HashMap<String, String>(); public DatabaseJSON(){ databases.put("MySQL", "MySQL"); databases.put("Oracle", "Oracle"); databases.put("PostgreSQL", "PostgreSQL"); databases.put("Microsoft SQL Server", "Microsoft SQL Server"); databases.put("DB2", "DB2"); databases.put("Others", "Others"); } public String execute() { return Action.SUCCESS; } public Map<String, String> getDatabases() { return databases; } public void setDatabases(Map<String, String> databases) { this.databases = databases; } } A normal Action class, just doing the redirect work and store the autocompleter value. package com.mon.action; import com.opensymphony.xwork2.ActionSupport; public class AutoCompleterAction extends ActionSupport{ private String yourDatabase; public String display() { return NONE; } public String getYourDatabase() { return yourDatabase; } public void setYourDatabase(String yourDatabase) { this.yourDatabase = yourDatabase; } } 3. ResultA bit tricky here, use a “s:url” tag point to a “databaseJSON” action, which will return a list of the option in JSON format. And link it to the autocompleter component via href=”%{databaseList}”. <%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head> <body> <h1>Struts 2 autocompleter + JSON example</h1> <s:form action="resultAction" namespace="/" method="POST" > <s:url id="databaseList" action="databaseJSON" /> <sx:autocompleter label="What's your favorite Database Server?" href="%{databaseList}" name="yourFavDatabase" /> <s:submit value="submit" name="submit" /> </s:form> </body> </html> 4. struts.xmlConfigure the Action and JSON provider as following : <param name=”root”>databases</param> It means, convert the DatabaseJSON’s databases property into JSON format, but the entire object. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts./dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="json" namespace="/" extends="json-default"> <action name="databaseJSON" class="com.mon.action.DatabaseJSON"> <result type="json" > <param name="root">databases</param> </result> </action> </package> <package name="default" namespace="/" extends="struts-default"> <action name="autoCompleterAction" class="com.mon.action.AutoCompleterAction" method="display"> <result name="none">pages/autocompleter-json.jsp</result> </action> <action name="resultAction" class="com.mon.action.AutoCompleterAction" > <result name="success">pages/result.jsp</result> </action> </package> </struts> 4. DemoAccess the action URL, now the autocompleter select options is provided by the JSON data. http://localhost:8080/Struts2Example/autoCompleterAction.action Alternatively, you can access the JSON data directly via the following URL { "PostgreSQL":"PostgreSQL", "MySQL":"MySQL", "Others":"Others", "Oracle":"Oracle", "Microsoft SQL Server":"Microsoft SQL Server", "DB2":"DB2" } Reference |
|
来自: LibraryPKU > 《J2EE》