分享

Struts 2 autocompleter + JSON example

 LibraryPKU 2013-11-19
By

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.
  1. Struts 2 AutoCompleter example
  2. Struts 2 JSON example

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.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.
AutoCompleterAction.java

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. 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 :

<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. Demo

Access the action URL, now the autocompleter select options is provided by the JSON data.

http://localhost:8080/Struts2Example/autoCompleterAction.action

Struts 2 AutoCompleter JSON example

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"
}

Reference

  1. Struts 2 JSON plugin
  2. JSON official documentation
  3. Struts 2 autocompleter example
  4. Struts 2 JSON example

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多