<struts:autocompleter>,autocompleter标签具有自动完成功能的下拉框,能根据所填写的内容筛选下拉框的内容。使用autocompleter标签必须使用Ajax主题,因为它用到了DOJO的JavaScript库。
autocompleteAjaxTag.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="/struts-tags" prefix="struts"%> <struts:head theme="ajax" /> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <struts:url id="dataUrl" value="/autocompleteAjaxData1.jsp" /> <!-- 数据URL --> <struts:autocompleter name="country" theme="ajax" href="%{dataUrl}" loadOnTextChange="true" loadMinimumCount="0" autoComplete="false" showDownArrow="true" indicator="indicator" /> <img id="indicator" src="images/loading.gif"> </body> </html>
autocompleteAjaxData1.jsp
获取前缀,找到前缀匹配项,封装成JSONArray,然后回传。
<%@page import="java.util.List"%> <%@page import="java.util.ArrayList"%> <%@page import=" net.sf.json.JSONArray"%> <%@page import=" java.io.IOException"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% out.clear();//清空输出数据 request.setCharacterEncoding("UTF-8");//设置request编码 response.setHeader("Pragma", "no-cache");//禁止缓存 response.setHeader("cache-control", "no-cache"); response.setDateHeader("Expires", 0); List<String> countries = new ArrayList<String>(); countries.add("Afghanistan"); countries.add("Aland Islands"); countries.add("Albania"); countries.add("Algernia"); countries.add("American Samno(USA)"); countries.add("Andorra"); String countryPrefix = request.getParameter("country");//获取AutoCompleter已经填写的内容 if (null == countryPrefix) { countryPrefix = ""; } List<String> buffer = new ArrayList<String>(); for (int i = 0; i < countries.size(); i++) { if (countries.get(i).toLowerCase() .startsWith(countryPrefix.toLowerCase())) { buffer.add(countries.get(i)); } } JSONArray jsonArray = JSONArray.fromObject(buffer); Thread.sleep(500);//当前线程睡眠0.5秒,演示indicator try { response.setContentType("aplication/json;charset=UTF-8"); out.print(jsonArray); } catch (IOException e) { e.printStackTrace(); } %>
struts2 autocompleter 动态下拉菜单
时间: 2024-10-09 22:56:31