Implement database-backed dynamic autocomplete search in Apache Struts 2 using jQuery UI and an Oracle database backend.
In this JavaAnt guide, we walk through the implementation step by step.
Autocomplete is a common requirement in web projects. While static autocomplete in jQuery is straightforward, implementing dynamic autocomplete in Apache Struts 2 where suggestions are fetched from an Oracle database is also simple once the pieces are connected.
Follow the steps below to implement dynamic autocomplete in Struts 2 with jQuery UI.
Step 1: Project Setup and Dependencies
Create a Dynamic Web Project in Eclipse using Java 7 or newer. The required libraries include:
- Standard Struts 2 core JARs (
struts2-core,xwork-core,ognl,freemarker,commons-io,commons-fileupload) - Jackson mapper JARs (
jackson-core-asl,jackson-mapper-asl) for generating JSON responses - Oracle JDBC driver (
ojdbc6.jar)
Step 2: Create the JSP Views
We create two JSP files in the WebContent/pages/ directory:
index.jsp: Sends the autocomplete query as the user types in the input box.country.jsp: Returns the filtered JSON response containing matched country names.
index.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#country").autocomplete({
source: function(request, response) {
$.ajax({
url: "getcountry.action",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function(data) {
response($.map(data, function(item) {
return { label: item, value: item };
}));
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('Error fetching suggestions: ' + textStatus);
}
});
},
minLength: 2
});
});
</script>
</head>
<body>
<h2>Struts 2 Dynamic Autocomplete</h2>
<s:form action="success">
<s:textfield name="country" id="country" label="Country Name" />
</s:form>
</body>
</html>country.jsp
Notice that country.jsp does not have any HTML wrapper tags; it writes raw JSON output using Jackson's ObjectMapper:
<%@ page import="java.util.List,org.codehaus.jackson.map.ObjectMapper" %>
<%
List countryList = (List) request.getAttribute("countyList");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(countryList);
out.println(json);
%>Step 3: Create the Actions and DAO
Create two action classes and one DAO:
AutoCompleteAction.java: Forwards user toindex.jspon initial page load.CountryAction.java: Reads the search parameter (term), callsCountryDAOto query Oracle, and places the matched country names in the request scope.CountryDAO.java: Connects to Oracle via JDBC and runs aLIKE ?query on thecountrytable.
Step 4: Database Schema (db.sql)
Create the table and insert sample country records into the Oracle database:
drop table country;
create table country(
countryname varchar2(50),
countrycode varchar2(5)
);
insert into country(countryname, countrycode) values('Afghanistan', '93');
insert into country(countryname, countrycode) values('Albania', '355');
insert into country(countryname, countrycode) values('Algeria', '213');
insert into country(countryname, countrycode) values('American Samoa', '1684');
insert into country(countryname, countrycode) values('Andorra', '376');
insert into country(countryname, countrycode) values('Angola', '244');
insert into country(countryname, countrycode) values('Anguilla', '1264');
insert into country(countryname, countrycode) values('Antarctica', '672');
insert into country(countryname, countrycode) values('India', '91');
insert into country(countryname, countrycode) values('Indonesia', '62');
commit;Step 5: Configure struts.xml
Define the action mappings in src/struts.xml:
<?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="default" namespace="/" extends="struts-default">
<action name="autocomplete" class="com.codewale.example.action.AutoCompleteAction">
<result name="success">/pages/index.jsp</result>
</action>
<action name="getcountry" class="com.codewale.example.action.CountryAction">
<result name="country">/pages/country.jsp</result>
</action>
</package>
</struts>Step 6: Test the Application
Deploy the application to Apache Tomcat and visit http://localhost:7001/StrutsAutocomplete/autocomplete. Type "al" into the country input field to observe suggestions dynamically populated from Oracle.