软体印象

Kavin
posts - 13, comments - 0, trackbacks - 0, articles - 0

Auto complete with Jquery

Posted on 2012-06-07 22:39 Kavin 阅读(297) 评论(0)  编辑  收藏

1. config servlet in web.xml
 <servlet>
     <servlet-name>addressData</servlet-name>
     <servlet-class>com.xxx.webapp.AddressDataAutoCompleteServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
    </servlet>
   
2. set autocomplete in JSP
$().ready(function() {
 
 $("#addressLine1").autocomplete("<%=StrUtils.filterStr(request.getContextPath())%>/servlet/addressData?paramName=addressLine");
 $("#addressLine2").autocomplete("<%=StrUtils.filterStr(request.getContextPath())%>/servlet/addressData?paramName=addressLine2");
 $("#addressLine3").autocomplete("<%=StrUtils.filterStr(request.getContextPath())%>/servlet/addressData?paramName=addressLine3");
 $("#addressCity").autocomplete("<%=StrUtils.filterStr(request.getContextPath())%>/servlet/addressData?paramName=addressCity");
 $("#addressPostCode").autocomplete("<%=StrUtils.filterStr(request.getContextPath())%>/servlet/addressData?paramName=addressPostCode");
 $("#firstFocusObj").focus();
});

<html:text name="" tabindex="9" style="color: grey;" onclick="clearAddressValue(this)" styleId="addressLine1" value='<%=request.getAttribute("postalAddress1") == null ? defalutAddressValue : (String)request.getAttribute("postalAddress1")%>' property="TLContactDetailForm.postalAddress1" size="50" maxlength="50" styleClass="inputText2"></html:text>

<input type="text" name="TLContactDetailForm.postalAddress1" maxlength="50" size="50" tabindex="9" value="Start typing your address and we will finish it for you" onclick="clearAddressValue(this)" id="addressLine1" style="color: grey;" class="inputText2">

3. return addresses in java class: AddressDataAutoCompleteServlet.java

 

package com.xxxxxx.webapp;

import com.xxxxxx.exception.ServiceException;
import com.xxxxxx.util.StrUtils;
import com.xxxxxx.webapp.context.ServiceLocator;
import com.xxxxxx.util.AddressShow;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;


/**
 * Servlet implementation class AddressDataAutoCompleteServlet
 */
public class AddressDataAutoCompleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public Logger LOG = Logger.getLogger(this.getClass().getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddressDataAutoCompleteServlet() {
        super();
    }
   
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        String paramName = request.getParameter("paramName");
        String paramValue = request.getParameter("q");
        LOG.info(paramName);
        LOG.info(paramValue);

        List list = new ArrayList();

        try {
            list = ServiceLocator.getAddressService()
                                 .searchAddressList(paramName, paramValue);
        } catch (ServiceException e) {
         LOG.error(e.getMessage(),e);
        }

        PrintWriter t_PW = response.getWriter();
       
        for (int i = 0; i < list.size(); i++) {
            String address = StrUtils.toTitleCase((String) list.get(i));

            if (paramName.equals("addressLine")) {
             AddressShow.showAddress(t_PW,address);
            } else if (paramName.equals("addressLine1")) {
             AddressShow.showAddress(t_PW,address);
            } else if (paramName.equals("addressLine2")) {
             AddressShow.showAddress(t_PW,address);
            } else if (paramName.equals("addressLine3")) {
             AddressShow.showAddress(t_PW,address);
            } else if (paramName.equals("addressCity")) {
             AddressShow.showAddress(t_PW,address);
            } else if (paramName.equals("addressPostCode")) {
             AddressShow.showAddress(t_PW,address);
            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

 

 


------
AddressShow.java

package com.intl.cigna.util;

import java.io.PrintWriter;

public class AddressShow
{
  public static void showAddress(PrintWriter pw, String input)
  {
    pw.write(input + "\n");

  }
}


只有注册用户登录后才能发表评论。


网站导航: