javaajaxjspstruts2struts2-jquery

Autocomplete textbox using Ajax and Oracle database in Struts 2


I am trying to populate my textbox with values coming from the jobs table in Oracle database, but I am not able to do it so, and not able to understand where I am making a mistake. Below is my code.

Jobs.java:

package action;

public class Jobs {
private String title;

    public Jobs(String title) {
        this.title = title;
    }

   

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

   

   
          

AjaxAction.java:

package action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import oracle.jdbc.rowset.OracleCachedRowSet;

public class AjaxAction extends ActionSupport{
 //ArrayList<Jobs> jobs=new ArrayList<Jobs>();
    ArrayList<String> jobs=new ArrayList<String>();

    public ArrayList<String> getJobs() {
        return jobs;
    }

    public void setJobs(ArrayList<String> jobs) {
        this.jobs = jobs;
    }
   

    private String country;
 
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
   
    public String execute() throws Exception
    {
         OracleCachedRowSet crs=new OracleCachedRowSet();
               crs.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
       
        crs.setUsername("**");
        crs.setPassword("***");
        crs.setCommand("select job_title from jobs");
       
        crs.execute();
       while(crs.next())
       {
           jobs.add(crs.getString("job_title"));
          
       }
      System.out.println(jobs);
       return "success";
    }
  

   
}

AjaxDemo.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="jq" uri="/struts-jquery-tags"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Ajax Page</title>
        <s:head/>
    </head>
    <body>
       JOB TITLE <jq:autocompleter size="1" list="jobs" name="country">
            
        </jq:autocompleter>
      
    </body>
</html>

Solution

  • <%@taglib prefix="sj" uri="/struts-jquery-tags"%>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Ajax Page</title>
        <sj:head/>
    </head>
    

    The sj:head tag loads jquery library.