Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Using Struts 2 Autocompleter With Example

Struts » On Oct 29, 2011 1 Comment

Let us see how to work with struts 2 autocompleter tag with example.  This autocompleter tag will works asynchronously, and dont forget to add <s:head theme=”ajax” /> in the header part of the webpage [ check in success.jsp ].

jars required

  • commons-logging-1.0.4.jar
  • freemarker-2.3.8.jar
  • ognl-2.6.11.jar
  • struts2-core-2.0.11.jar
  • xwork-2.0.4.jar

Example

files required

  • index.jsp
  • success.jsp
  • web.xml
  • struts.xml
  • LogingEx.java

Directory Structure

index.jsp

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=iteAction.action">

success.jsp

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

  <head>
  <s:head theme="ajax" />
  </head>

  <s:label value="Your State" />
  <s:autocompleter theme="simple" list="states"/>

LogingEx.java

package java4s;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private List states;

        public List getStates()
        {
              return states;
        }

      public String execute() throws Exception{
      states = new ArrayList();
      states.add("Alabama");
      states.add("Alaska");
      states.add("Arizona");
      states.add("Arkansas");
      states.add("California");
      states.add("Colorado");
      states.add("Connecticut");
      states.add("Delaware");
      states.add("District of Columbia");
      states.add("Florida");
      states.add("Georgia");
      states.add("Hawaii");
      states.add("Idaho");
      states.add("Illinois");
      states.add("Indiana");
      states.add("Iowa");
      states.add("Kansas");
      states.add("Kentucky");
      states.add("Louisiana");
      states.add("Maine");
      states.add("Maryland");
      states.add("Massachusetts");
      states.add("Michigan");
      states.add("Minnesota");
      states.add("Mississippi");
      states.add("Missouri");
      states.add("Montana");
      states.add("Nebraska");
      states.add("Nevada");
      states.add("New Hampshire");
      states.add("New Jersey");
      states.add("New Mexico");
      states.add("New York");
      states.add("North Carolina");
      states.add("North Dakota");
      states.add("Ohio");
      states.add("Oklahoma");
      states.add("Oregon");
      states.add("Pennsylvania");
      states.add("Rhode Island");
      states.add("South Carolina");
      states.add("South Dakota");
      states.add("Tennessee");
      states.add("Texas");
      states.add("Utah");
      states.add("Vermont");
      states.add("Virginia");
      states.add("Washington");
      states.add("West Virginia");
      states.add("Wisconsin");
      states.add("Wyoming");
      return SUCCESS;
      }
    }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>

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>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="iteAction" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

Output

 

What you are thinkig....

One Response to “Using Struts 2 Autocompleter With Example”
  1. Maura Dobberstein says:

    I enjoy the efforts you have put in this, appreciate it for all the great posts.

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Servlets, Jsps
is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2013 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by str-Graphics.com