jQuery.Autocomplete 与struts2的整合

 

需要做一个text的自动提示功能,于是翻翻jquery 的书,看了一个叫做autocomplete的插件,决定试一试怎么使用.上google搜索了一下插件(开始搜索的是山寨版本的,导致自己的ajax无法调用action,那个郁闷啊).最后还是去jquery的官网上,下载了demo和源码.闲话不说,开始写代码:

1.html页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>Auto Complete 4.1 (backwards compatible file)</title>
  <script type='text/javascript' src='js/jquery-1.4.2.js'></script>
  <script type='text/javascript' src='js/jquery.autocomplete.js'></script>
  <link rel='stylesheet' type='text/css'  href='style/jquery.autocomplete.css' />

  <script type='text/javascript'>
  $(document).ready(function()
  {
   $("#queryCon").autocomplete("autoComplete.action",
     {
           minChars: 1,
           max:5,
           width: 150,
           matchContains: true,
           autoFill: false,

           //如果需要另外传值给action的话可以用这样的方式,如果只是把input里面的值传递给action的话,可以不写 
           extraParams:
        {  
              queryCon: function()
              {
               return $("#queryCon").val();
              }  
           },

           //进行对返回数据的格式处理

           parse: function(test)
           {
               //content为我action里面定义的值

               data = test.content;
               var rows = [];
             
               for(var i=0; i<data.length; i++)
            {
                 rows[rows.length] =
              {
                   //我没有特殊的要求,所以三个写的一样                  

                   data:data[i],
                   value:data[i],

                  //result里面显示的是要返回到列表里面的值 
                   result:data[i]
                 };
               }          
               return rows;
           },
           formatItem:function(item)
           {

                   //我没有特殊的要求,直接返回了
                   return item;
           }

      });   
  });
  </script>
 </head>
 <body>
   <input type='text' style='width: 300px;' id='queryCon'
    name="queryCon" />
   <input type='submit' value="All Values" />

 </body>
</html>

2.action部分

package com.training.util;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionSupport;
import com.training.hibernate.User;

public class AutoCompleteAction extends ActionSupport {
 private List<String> content;

 public List<String> getContent() {
  return content;
 }

 public void setContent(List<String> content) {
  this.content = content;
 }

 private String queryCon;

 public void setQueryCon(String queryCon) {
  this.queryCon = queryCon;
 }

 public String getQueryCon() {
  return queryCon;
 }

 @Override


 public String execute() throws Exception {

//回出现中文乱码传值的问题,所以需要再编码

,不过很奇怪,我写了filter了,不知道为什么还需要这样
  queryCon = new String(queryCon.getBytes("ISO-8859-1"), "UTF-8");

  Session session = HibernateSessionFactory.getSession();
  Query query = session
    .createQuery("select name from User as user where user.name like ? ");


  query.setString(0, this.queryCon + "%");

  List<String> list = query.list();

  this.setContent(list);
  session.close();

  return SUCCESS;
 }

}

3.struts.xml

<package name="login" extends="json-default">
  <action name="autoComplete" class="com.training.util.AutoCompleteAction">
   <result type="json"/>
  </action>
 </package>

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/huangxu0311/archive/2010/04/28/5539413.aspx

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值