Example of autocompleter in struts2.2.1
Posted on: January 19, 2011 at 12:00 AM
In this example, you will see the use of autocompleter tag of struts2.2.1.

Example of autocomplete in struts2.2.1

In this example, you will see the use of  autocomplete tag of  struts2.2.1. It is a combo box that will a display drop down suggestion lists while user enter on the text box.

 1- index.jsp

<%@taglib uri="/struts-tags" prefix="s" %>


<head> <title>AutoComplete_TagExample</title> </head>



<s:a href="autoCompleteAction.action">AutoComplete_TagExample</s:a>



2_ ListOfMonth.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>


<head><title>AutoComplete_TagExample</title><sx:head /></head>


<s:form action="resultAction.action" >

<sx:autocompleter size="1" label="Month" 

list="nameMonths" name="month"></sx:autocompleter>


<s:submit align="left"> </s:submit>



3_ AutoCompleteAction.java

package roseindia.action;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class AutoCompleteAction extends ActionSupport {
private String password = "root";
private String user = "root";
private String url = "jdbc:mysql://";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
private List<String> nameMonths = new ArrayList<String>();
private String month;

public AutoCompleteAction() throws SQLException {
try {
con = DriverManager.getConnection(url, user, password);
stmt = con.createStatement();
String sql = "select nameMonths from monthsName";
rs = stmt.executeQuery(sql);
while (rs.next()) {

} catch (ClassNotFoundException e) {



public String getMonth() {
return month;

public void setMonth(String month) {
this.month = month;

public List<String> getNameMonths() {
return nameMonths;

public void setNameMonths(List<String> nameMonths) {
this.nameMonths = nameMonths;

public String execute() throws Exception {

return SUCCESS;

public String display() {
return NONE;

4_ OutListOfMonth.jsp


<constant name="struts.devMode" value="true" />

<package name="roseindia" extends="struts-default">

<action name="autoCompleteAction" class="roseindia.action.AutoCompleteAction" method="display">

<result name="none">jspPages/ListOfMonth.jsp</result>


<action name="resultAction" class="roseindia.action.AutoCompleteAction">

<result name="success">jspPages/OutListOfMonth.jsp</result>




4_ OutListOfMonth.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 


<%@ taglib prefix="s" uri="/struts-tags"%>





Selected Month :<s:property value="month" />







Download Select Source Code

Related Tags for Example of autocompleter in struts2.2.1: