Struts 2 autocompleter + JSON example
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.
Struts 2 autocompleter + JSON example
In 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 library
Get 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. Action
A class to convert into JSON format later, to provide a list of the select options to the autocompleter component.
DatabaseJSON.java
package com.mkyong.common.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.
AutoCompleterAction.java
package com.mkyong.common.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. Result
A 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.xml
Configure the Action and JSON provider as following :
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.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="json" namespace="/" extends="json-default"> <action name="databaseJSON" class="com.mkyong.common.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.mkyong.common.action.AutoCompleterAction" method="display"> <result name="none">pages/autocompleter-json.jsp</result> </action> <action name="resultAction" class="com.mkyong.common.action.AutoCompleterAction" > <result name="success">pages/result.jsp</result> </action> </package> </struts>
4. Demo
Access 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
http://localhost:8080/Struts2Example/databaseJSON.action
{ "PostgreSQL":"PostgreSQL", "MySQL":"MySQL", "Others":"Others", "Oracle":"Oracle", "Microsoft SQL Server":"Microsoft SQL Server", "DB2":"DB2" }






The tag
should be changed to
Because in AutoCompleterAction, the property is “yourDatabase”, not “yourFavDatabase”.
thanks for the help….!!!!
Impressed with your quick reply.
I want to integrate reporting with Struts2 app. can you give me some suggestion. Birt or jasper?
and how to integrate it with struts2 app. My requirment is reports with custom header and footer with rtf,excel and pdf export
[...] http://localhost:8080/Struts2Example/autoCompleterAction.action Here’s another example to show the use of JSON data to provide a list of the select options to the autocompleter component – Struts 2 autocompleter + JSON example. [...]
I’m impressed! You’ve managed the almost iopmsisble.