Suggest tutorial

Introduction

The Struts-Layout Suggest tag allows to render a text field with a suggestions list: when a user types a character, the tag shows suggestions matching typed word, and the user just have to use keyboard or mouse in order to choose one of these suggestions.

Creating a Suggest field

Here are the required steps to create a suggest field:

  • Creation of a particular Struts Action class
  • Configuration of this action
  • Creation of the JSP

This tutorial is based on the demo application named CountrySuggest, which shows an example of a suggest field allowing to type easily country names. Here is the result:

Creation of a particular Struts Action class

In fact, when a user types a character, a Struts action is called. This action class has two particularities:

  • It must extends the fr.improve.struts.taglib.layout.suggest.SuggestAction class.
  • It must implement an inherited abstract method named getSuggestionList(HttpServletRequest in_request, String in_word): this method returns a suggestions list (as a String Collection) from the typed word (as in_word).
 1package fr.improve.demosuggest.countries;
 2
 3import java.util.ArrayList;
 4import java.util.Collection;
 5import java.util.Iterator;
 6import javax.servlet.http.HttpServletRequest;
 7import fr.improve.struts.taglib.layout.suggest.SuggestAction;
 8
 9public class CountrySuggestAction extends SuggestAction {
10
11    public Collection getSuggestionList(HttpServletRequest in_request, String in_word) {
12
13        // Get all the country names
14        Collection allCountries = CountryCollection.getAllCountries();
15
16        // Start to build the suggestions list
17        ArrayList suggestions = new ArrayList();
18
19        if (in_word != null && in_word.length() > 0)
20        {
21            Iterator iter = allCountries.iterator();
22
23            while(iter.hasNext())
24            {
25                String currentWord = (String) iter.next();
26
27                if(currentWord.toLowerCase().startsWith(in_word.toLowerCase()))
28                    suggestions.add(currentWord);
29            }
30        }
31
32        return suggestions;
33    }
34
35}            

Note that this code is based on a class named CountryCollection providing a method called getAllCountries() returning all country names.

Configuration of this action

Just add these lines into your struts-config.xml file (in the <action-mappings> ... </action-mappings> part):

 1<struts-config>
 2
 3    ...
 4
 5    <action-mappings>
 6
 7        <action path="/getCountrySuggestions" 
 8            type="fr.improve.demosuggest.countries.CountrySuggestAction">
 9        </action>
10
11    </action-mappings>
12
13    ...
14
15</struts-config>

Creation of the JSP

Finally just add the <layout:suggest> tag into a JSP page:

1<%@ taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
2
3<layout:html>
4
5    <layout:suggest suggestAction="/getCountrySuggestions" key="Country" styleId="myTextField" value="" suggestCount="8" />
6
7</layout:html>

Note that you can also specify the 'property' attribute in order to bound this field to a property of an action form.

suggest.png (2.7 kB) Frédéric ESNAULT, 02/28/2012 02:44 pm