在Struts 2中使用ajax和oracle数据库自动完成文本框

时间:2015-07-18 18:42:56

标签: java ajax struts2 struts2-jquery

我正在尝试使用来自Oracle数据库中的jobs表的值来填充我的文本框,但是我无法这样做,并且无法理解我在哪里犯了错误。以下是我的代码。如果有人能提供帮助,我们将不胜感激

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>

1 个答案:

答案 0 :(得分:0)

git submodule add --depth 1 git://github.com/RestKit/RestKit.git

<%@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> 标记加载jquery库。