Creating an Autocomplete Textbox
One of the cool tags of AjaxTags is ajax:autocomplete. We will use this tag in this following JSP example.
<fieldset>
<legend>ajax:autocomplete example</legend>
<form>
<label for="country">Country Name: </label>
<input type="text" name="country" id="country"/>
</form>
<ajax:autocomplete
source="country"
target="country"
baseUrl="country_list.view"
parameters="country={country}"
className="autocomplete"
minimumCharacters="1"/>
</fieldset>
Our objective here is to make an autocomplete for the textbox country. We have a Servlet which will return a list of country names that starts with the value of country textbox.
This tag must appear after the form tag. Attribute source is the id of the input textbox. Attribute target is the id of the target textbox; target may be same as source or different than source. Attribute baseUrl specifies the server side code. One important attribute for ajax:autocomplete is minimumCharacters, it specifies character count after which AjaxTags will request for country_list.view with country name as a parameter.
.autocomplete {
position: absolute;
color: #333333;
background-color: #ffffff;
border: 1px solid #666666;
font-family: Arial;
overflow: hidden;
}
This is a sample style sheets entry for ajax:autocomplete.
Now we need to create the server response for this tag. Sample response would be like following. Assuming we entered ‘A’ in country textbox.
<?xml version="1.0" encoding="UTF-8"?>
<ajax-response>
<response>
<item>
<name>America</name>
<value>America</value>
</item>
<item>
<name>Angola</name>
<value>Angola</value>
</item>
<item>
<name>Austria</name>
<value>Austria</value>
</item>
<item>
<name>Australia</name>
<value>Australia</value>
</item>
</response>
</ajax-response>
In Part I, I discussed AjaxTags helper classes. First we need to add following import statements in your Servlet code.
import org.ajaxtags.helpers.*;
import org.ajaxtags.servlets.*;
Next you need to ... [Read Full Article]
click here to see original article or to find similar articles
HEADLINES
Forces align against GOP in Senate races
Even the top Republican in charge of the party's Senate campaigns concedes that the GOP will lose seats this year — the only question is how many.
Sudans president attends Africa summit in Turkey
(AP)
AP - Sudanese President Omar al-Bashir attended a summit of African leaders in Turkey on Tuesday in his first trip abroad since an international court indicted him on genocide charges last month.
Pakistani coalition focuses on security, economy
(Reuters)
Reuters - Leaders of Pakistan's coalition
government set about tackling pressing economic and security
problems on Tuesday as a bomb at a hospital in a northwestern
town killed 20 people.