1. First introduce the jquery UI-related JS, such as: Jquery-ui-1.10.4.js
2. Write JS
<Scripttype= "Text/javascript">$(function() {setautocomplete ();});/*Auto-Complete parts number*/functionSetautocomplete () {$ ('. Class_name'). each (function() {$ ( This). AutoComplete ({Source:"/admin/class_getclassnamelist.do",
Minimum number of user input minLength:4}); });}</Script>
3. Write Java code:
(1). Action:
/*** Get class list * @date July 11, 2017 pm 1:24:22 *@authorSongchunyan *@return */ PublicString getclassnamelist () {Try { //parameter, the default value is termString input = request.getparameter ("term"); List<String> partnolist =eltrainclassservice.getnmdyclassnamelist (input); String JSON=jsonutil.serialize (partnolist); This. writehtml (JSON); return NULL; } Catch(dolexception e) {logger.error (E.getmessage (), E); returnhandledolexception (e); } Catch(Exception e) {logger.error (E.getmessage (), E); returnHandleException (e); } }
(2). Service:
/*** Get class drop-down list * @date July 11, 2017 pm 1:26:42 *@authorSongchunyan *@paramInput *@return * @throwsException*/ Public Staticlist<string> getnmdyclassnamelist (String input)throwsexception{dbbase DB=NULL; Try{db=Newdbbase (); returneltrainclassdao.getnmdyclassnamelist (db,input); } Catch(Exception e) {logger.error ("Getnmdyclassnamelist Error:" +e.tostring (), E); Throwe; } finally { if(db! =NULL) {db.release (); } } }
3.dao:
/*** Get class drop-down list * @date July 11, 2017 pm 1:28:14 *@authorSongchunyan *@paramDB *@paramInput *@return * @throwsException*/ Public StaticList<string> getnmdyclassnamelist (dbbase db, String input)throwsexception{StringBuffer SQL=NewStringBuffer (); Sql.append ("Select Train_class_uid,train_class_name from El_tms_offline_train_class etotc,tbl_research TR"); Sql.append ("WHERE Etotc.train_uid = Tr.train_uid"); Sql.append ("And Etotc.begin_time <= Now ()"); Sql.append ("and tr.isnmdy= ' Y '"); Sql.append ("and train_class_name like concat ('% ',?, '% ')"); Sql.append ("Limit 10"); Db.initps (Sql.tostring ()); Db.setpsstring (1, input); ResultSet RS=Db.executequery (); List<String> list =NewArraylist<string>(); while(Rs.next ()) {//cu = new Eltrainclass (); //Cu.settrainclassuid (rs.getstring ("Train_class_uid")); //Cu.settrainclassname (rs.getstring ("Train_class_name"));List.add (rs.getstring ("Train_class_name")); } rs.close (); returnlist; }
The JQuery UI completes the drop-down list of auto-matching steps