Searching...
Wednesday, August 20, 2014

Strut2-Autocomplete-With-Database

August 20, 2014
Strut2-Autocomplete-With-Database




index.jsp

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

Autocomplate.jsp

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

    pageEncoding="ISO-8859-1"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

  <s:head theme="ajax" />

</head>

<body>



 <center><h1>Strut2-Autocomplete-With-Database</h1><br>

 <s:label value="States" />

  <s:autocompleter theme="simple" list="states" size="20" /></center>

 

</body>

</html>


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="autocomplate" class="java9r.com.Autocomplate">

            <result name="success">/Autocomplate.jsp</result>          

        </action>

    </package>

</struts>

Autocomplate.java

package java9r.com;

import java.util.ArrayList;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;



public class Autocomplate extends ActionSupport{   

    private static final long serialVersionUID = 1L;

   

    private List<String> states;

   

        public List<String> getStates()

        {

              return states;

        }

       

        public String execute() throws Exception{

        Connection con = null;

        try{

           

            Class.forName("com.mysql.jdbc.Driver");

             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","ravi");

             Statement st=con.createStatement();

             ResultSet rs = st.executeQuery("select state from state");

       

             List<String> statelist = new ArrayList<String>();

            

               while(rs.next())

               {

                   statelist.add(rs.getString(1));

               }

       

                   

               states = new ArrayList<String>(statelist);

                  

     

        }catch(SQLException e){

            e.printStackTrace();

        }catch(Exception e){

            e.printStackTrace();

        }

       

        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>




                                                                        Download  3.9MB

0 comments:

Post a Comment

ads2