zoukankan      html  css  js  c++  java
  • 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>
     
  • 相关阅读:
    BZOJ 1529 [POI2005]ska Piggy banks:并查集
    BZOJ 1370 [Baltic2003]Gang团伙:并查集【虚点】
    BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】
    BZOJ 1660 [Usaco2006 Nov]Bad Hair Day 乱发节:单调栈
    BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】
    BZOJ 1638 [Usaco2007 Mar]Cow Traffic 奶牛交通:记忆化搜索【图中边的经过次数】
    BZOJ 1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏:新版floyd【路径上最大边最小】
    BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
    POJ2226-Muddy Fields-二分图*
    POJ2186-Tarjan-kosaraju-缩点
  • 原文地址:https://www.cnblogs.com/passer1991/p/2770833.html
Copyright © 2011-2022 走看看