jQuery.Autocomplete与struts2的整合

ssh中jquery autocomplete使用示例

jQuery.Autocomplete与struts2的整合

 

以下是我的实践,不过不知道为什么autocomplete功能不能从返回的json对象中选择包含输入字段的一项,例如source:person!ajaxSelectPerson ,这个时候person! ajaxSelectPerson返回的json,当输入wang时,当source: ["张三","李斯","王五","赵六","黑龙","白海","湘伦"]时

 

package com.maple.action;
 
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
 
  import net.sf.json.JSONArray;
 
  import org.apache.struts2.ServletActionContext;
  import org.apache.struts2.convention.annotation.ResultPath;
 
  import com.opensymphony.xwork2.ActionSupport;
 
 @ResultPath("/")
  // @Results({@Result(name="")})
  public  class PersonAction  extends ActionSupport{
      public String result;
      public String getResult() {
          return result;
     }
      public  void setResult(String result) {
          this.result = result;
     }
      public String autocom()
     {
         result="[\"zhangsan\",\"lisi\",\"wangwu\",\"zhangsan\",\"sun\",\"zhouw\",\"hello\",\"world\",\"nihsi\"]";
          return "autocom";
     }
      public String ajaxSelectPerson(){ 
          try { 
         String search = ServletActionContext.getRequest().getParameter("term"); 
         System.out.println(search);
  //         if(search==null||search.length()==0) return null; 
         List<String> names =  new ArrayList<String>(); 
                            names.add("zhangsan"); 
                            names.add("lisi"); 
                            names.add("wangwu"); 
                            names.add("zhangsan"); 
                            names.add("sun"); 
                            names.add("zhouw"); 
                            names.add("hello"); 
                            names.add("world"); 
                            names.add("nihsi"); 
         ServletActionContext.getResponse().setCharacterEncoding("UTF-8"); 
         JSONArray json = JSONArray.fromObject(names); 
         PrintWriter out = ServletActionContext.getResponse().getWriter(); 
         out.print(json.toString()); 
         System.out.println( new Date()+"  "+json);
                 out.flush(); 
                 out.close(); 
                  return  null
         }  catch (Exception e) { 
         e.printStackTrace(); 
         } 
          return  null
     } 
 
 }

  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  <%
  String path = request.getContextPath();
  String basePath = request.getScheme()+ " :// "+request.getServerName()+ " : "+request.getServerPort()+path+ " / ";
  %>
 
  <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
  < html >
    < head >
      < base  href ="<%=basePath%>" >
        < link  rel ="stylesheet"  href ="development-bundle/themes/base/jquery.ui.all.css"   />
           < style >
     .ui-autocomplete 
{
         max-height
:  100px;
         overflow-y
:  auto;
         
/*  prevent horizontal scrollbar  */
         overflow-x
:  hidden;
     
}
     .ui-autocomplete 
{
         height
:  100px;
     
}
     
</ style >
      < script  src ="js/jquery-1.8.2.min.js" ></ script >
      < script  src ="js/jquery-ui-1.8.24.custom.min.js" ></ script >
      < script > source:"hello-world!returnData"
     $( function() {
          var availableTags = [${theData} ];
         $( "#tags" ).autocomplete({
             source: ["张三","李斯","王五","赵六","黑龙","白海","湘伦"] // "person!ajaxSelectPerson",//
         }); /**/
     });
      </ script >
  </ head >
  < body >
  
  < div  class ="ui-widget" >
      < label  for ="tags" >Tags:  </ label >
      < input  id ="tags"   />
  </ div >
    </ body >
  </ html >
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值