Use jquery. autocomplete. js to implement automatic prompts (similar to Google's)

Source: Internet
Author: User

In recent days, I have consistently looked at JSON. The so-called JSON, in fact, is also implemented by some JS functions at the underlying layer. Through it, I mainly perform asynchronous submission, transfer data at the underlying layer, and ignore the new page type. Of course, there are also many people who use ajax to implement some dynamic web pages. There are too many online materials. If you want to study it, hurry up, the following is an example of an automatic prompt in struts1 Based on the json I have read over the past two days:

 

First, the JS library introduced on the page:

<SCRIPT type = 'text/JavaScript 'src = 'njs/jquery-AutoComplete/jquery. autocomplete. js'> </SCRIPT>
<LINK rel = "stylesheet" type = "text/CSS" href = "NJS/jquery-AutoComplete/jquery.autocomplete.css"/>

 

Written script:

Jquery (document). Ready (function (){
Jquery ("# column04"). AutoComplete (// column04 represents the name of the text box
"Autocomplete_11.do ",
{
Multiple: True,
Width: 150,
MAX: 50,
Multipleseparator: '| ',
Datatype: 'json', // the data type is in JSON format
// Add a parsing function to the returned JSON object. The function returns an array.
Parse: function (data ){
// Alert (data );
VaR rows = [];
For (VAR I = 0; I <data. length; I ++ ){
Rows [rows. Length] = {
Data: Data [I],
Value: Data [I],
Result: DATA [I]
};
}
Return rows;
},
Formatitem: function (row, I, n ){
Return row;
}
}
);
})

 

 

// Struts-config.xml

<Action Path = "/autocomplete_11" parameter = "#" type = "com. sitech. ismp. iimp. common. action. autocompleteaction "Scope =" request "Validate =" false ">
<Forward name = "default" Path = "/"/>
</Action>

 

 

Action:

Public class autocompleteaction extends baseaction {

Public actionforward autocomplete_11 (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
String keyword = request. getparameter ("Q ");
Keyword = escapeunescape. Unescape (keyword );
System. Out. println ("q <:" + keyword );
Tblsys_loginmsg user = (tblsys_loginmsg) request. getsession ()
. Getattribute (tblsys_loginmsg.session_key );
String login_id = NULL;
If (user! = NULL )&&! User. Equals ("")){
Login_id = user. getlogin_id ();
}
Sys_loginmsgdao autocompletedao = new sys_loginmsgdao ();
List autocompletelist = autocompletedao. findtitleforjsname_deal (keyword, login_id );
System. Out. println ("list:" + autocompletelist. tostring ());
Try {
If (autocompletelist. Size ()> 0 ){
Response. setcontenttype ("text/html; charset = GBK ");
Printwriter out = response. getwriter ();
// System. Out. println ("Result:" + ibsmutil. getjsarrayfromlisttolist (autocompletelist, "titile "));
Jsonarray = jsonarray. fromobject (ibsmutil. getjsarrayfromlist_2 (autocompletelist, "titile "));
System. Out. println ("JSON array:" + jsonarray. Get (0 ));
Out. Print (jsonarray. tostring ());
// System. Out. println ("List Value:" + jsonarray. tostring ());
Out. Flush ();
Out. Close ();
}
Return NULL;
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
}

}

 

Convert to a string that can be converted to JSON format:

 

/**
*
*/
Package com. sitech. ismp. util. context;

Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;

Import com. sitech. ismp. Iimp. Common. Action. mytitle;

/**
* @ author xietb
*/
public class ibsmutil {
/*
* sets the sqlmap result list to JS array
*/
Public static string getjsarrayfromlist_2 (list result, string mapkey) {
stringbuffer SBF = new stringbuffer ();
SBF. append ("[");
for (INT I = 0; I hashmap map = (hashmap) result. get (I);
// system. out. println ("MAP value is:" + map. tostring ();
string value = map. get (mapkey ). tostring ();
// system. out. println ("value" + I + ":" + value);
if (I = 0) SBF. append ("/""). append (value ). append ("/" ");
else SBF. append (","). append ("/""). append (value ). append ("/" ");
}< br> SBF. append ("]");
return SBF. tostring ();
}< br> Public static void main (string [] ARGs ){
String stest = "Chinese 1234 ABCD [] () <+> ,.~ //";
System. Out. println (stest );
System. Out. println (escape (stest ));
System. Out. println (Unescape (escape (stest )));

}
}

 

 

You need to specify the following points:

1. The transmitted data must be transcoded and converted to UTF-8 format. Otherwise, the transmitted data is converted to JSON.

2. After obtaining the data, we need to convert the data into JSON data, which is actually a string, but it is best to convert the data into JSON format.

3. When retrieving data on the page, convert the data to JSON format, which is actually a simple JS array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.