Tuesday, 17 July 2012

Autocomplete Textbox in struts2

Autocomplete textbox using struts2 dojo plugin .
Add some jar files for autocomplete

Step2 Create java class AutocompleteAction   and add the line of code.


AutocompleteAction
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author sandy
 */
public class AutocompleteAction extends ActionSupport{
 
    private List<String> autoCompleteList;

    public List<String> getAutoCompleteList() {
        return autoCompleteList;
    }

    public void setAutoCompleteList(List<String> autoCompleteList) {
        this.autoCompleteList = autoCompleteList;
    }

 
    public String AutoComplete()
    {
    autoCompleteList = new ArrayList<String>();

 autoCompleteList.add("Delhi");
 autoCompleteList.add("Pune");
 autoCompleteList.add("Mumbai");
 autoCompleteList.add("Bangalore");

 autoCompleteList.add("Bhopal");
 autoCompleteList.add("Nagpur");
 autoCompleteList.add("Hyedrabad");
 autoCompleteList.add("Indore");

 autoCompleteList.add("Goa");
 autoCompleteList.add("Chennai");
 autoCompleteList.add("Kolkata");
 autoCompleteList.add("Mysore");

 autoCompleteList.add("Mangalore");
 autoCompleteList.add("Secondrabad");
 autoCompleteList.add("Kerla");
 autoCompleteList.add("Nashik");

 autoCompleteList.add("Rajkot");
 autoCompleteList.add("Surat");
 autoCompleteList.add("Barodra");
 autoCompleteList.add("Ahamdabad");

 return SUCCESS;
 
    }
}


step3 Create the jsp file file name AutoComplete.jsp and add the line of code.

AutoComplete.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib prefix="sx" uri="/struts-dojo-tags" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <sx:head/>
    </head>
    <body>
        <h1>Auto Complete</h1>
         My List : <sx:autocompleter list="autoCompleteList" name="myList"/>
    </body>
</html>

step 4 create the Struts .xml file and add the line of code
Struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!-- Configuration for the default package. -->
     <constant name="struts.devMode" value="true" />
      
    <package name="default" extends="struts-default" >
<action name="autocomplete" class="contact.action.AutocompleteAction" method="AutoComplete">
            <result name="success">/AutoComplete.jsp</result>
        </action>
    </package>
</struts>

Step 5 add some line of code inside the web.xml file 
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
  <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>
 <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/AutoComplete.jsp</welcome-file>
    </welcome-file-list>
</web-app>

step 5 Deploy the project on web server  .
type the url:http://localhost:8084/ContactDetails/autocomplete 





3 comments:

  1. how to show if there is no matching string for the typed??

    ReplyDelete
  2. this is not an example of autocomplete on textbox. it is on autocompleter , ie a modified combo box

    ReplyDelete
  3. I thing it will not execcute because u didn't give the action name on your jsp page..

    ReplyDelete