Tuesday, 3 December 2013

AutoComplete Example in Struts2

I am going to describe the autocompleter tag in struts2.This tag is similar to dropdown list but it is having some addition like user just enter the partial text and match with entered text automatically comes in the textbox.

Interesting Right?

There are the following step to implement it in struts2.

First you need to download the following jar files.
  • antlr-2.7.6.jar
  • commons-collections.jar
  • commons-fileupload-1.2.1.jar
  • commons-io-1.3.2.jar
  • commons-logging-1.0.4.jar
  • commons-logging-api-1.1.jar
  • freemarker-2.3.16.jar
  • log4j-1.2.14.jar
  • struts2-dojo-plugin-2.1.8.1.jar
  • ognl-3.0.jar
  • struts2-core-2.2.1.jar
  • xwork-core-2.2.1.jar
1. Create a basic web project in your IDE.
2. Open your web.xml file and do filterdispatcher entry in it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name>Struts 2 Web Application</display-name>
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>
 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
 </filter-class>
 </filter>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>
 
 3. Now create the struts.xml file inside the src folder.

<?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="autoCompleteAction"
 method="autoComplete">
 <result name="success">autocomplete.jsp</result>
 </action>
 </package>
 </struts>

 4. Now create the Struts2 Action class AutoCompleteAction.java and create method inside it.

package com.aoiblog.demo.actions;
 
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
 
public class AutoCompleteAction extends ActionSupport{
 
 private List<String> autoCompleteList;
 public String autoComplete(){
 autoCompleteList = new ArrayList<String>();
 
 autoCompleteList.add("aaaa aoiblog");
 autoCompleteList.add("abbb aoiblog");
 autoCompleteList.add("abcc aoiblog");
 autoCompleteList.add("abcd aoiblog");
 
 autoCompleteList.add("bbbb aoiblog");
 autoCompleteList.add("bccc aoiblog");
 autoCompleteList.add("bcdd aoiblog");
 autoCompleteList.add("bcde aoiblog");
 
 autoCompleteList.add("cccc aoiblog");
 autoCompleteList.add("cddd aoiblog");
 autoCompleteList.add("cdee aoiblog");
 autoCompleteList.add("cdef aoiblog");
 
 autoCompleteList.add("dddd aoiblog");
 autoCompleteList.add("deee aoiblog");
 autoCompleteList.add("deff aoiblog");
 autoCompleteList.add("defg aoiblog");
 
 autoCompleteList.add("eeee aoiblog");
 autoCompleteList.add("efff aoiblog");
 autoCompleteList.add("efgg aoiblog");
 autoCompleteList.add("efgh aoiblog");
 
 return SUCCESS;
 }
 
 public List<String> getAutoCompleteList() {
 return autoCompleteList;
 }
 
 public void setAutoCompleteList(List<String> autoCompleteList) {
 this.autoCompleteList = autoCompleteList;
 }
}
 
 5. Create the jsp file to show your autocomplete tag in it.

<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>autocomplete.jsp</title>
 <sx:head />
 </head>
 <body>
 My List : <sx:autocompleter list="autoCompleteList" name="myList"/>
 </body>
</html>

6. Now  your Autocomplete example application is ready .
Just deploy your project and run the following url on your browser.


http://localhost:8080/yourprojectname/autoCompleteAction.action
 
If you have any problem in it so please free feel to ask and put your problem in comment box.
Thanks to visit.

No comments:

Post a Comment