Skip to main content

Struts2 - Auto Completer Tag

In this section, we are going to describe the autocompleter tag. The autocompleter tag always displays a dropdown list with the options that have at least a partial match with entered text in the textbox. If the user clicks on the dropdown button then all options shown in the dropdown list. The autocompleter tag generates two input fields. First is "text", whose name is specified with the "name" attribute and another one is "hidden" whose name is "$(name). Key", where ${name} is the value in the "name" attribute.

The autocompleter tag loads its options asynchronously when the page loads suggested options based on the text entered by you in textbox. If the autoComplete attribute is set to 'true' (By defalut 'false') then it makes suggestions in the textbox.

[Note: When we use the "simple" theme, the autocompleter can be used like the ComboBox. When used on the "ajax" theme, the list can be retrieved from an action.]

Add the following code snippet into the struts.xml file.

struts.xml
<action name="autocompleter" class="net.struts2.autocompleter">
  <result>/pages/autocompleter.jsp</result>
</action>

Create a list in the action class and populate them with various states name of U.S. as shown in the "autocompleter" class.

package net.struts2;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class autocompleter extends ActionSupport{
  private List state;
  public String execute() throws Exception{
  state = new ArrayList();
  state.add("Alabama");
  state.add("Alaska");
  state.add("Arizona");
  state.add("Arkansas");
  state.add("California");
  state.add("Colorado");
  state.add("Connecticut");
  state.add("Delaware");
  state.add("District of Columbia");
  state.add("Florida");
  state.add("Georgia");
  state.add("Hawaii");
  state.add("Idaho");
  state.add("Illinois");
  state.add("Indiana");
  state.add("Iowa");  
  state.add("Kansas");  
  state.add("Kentucky");
  state.add("Louisiana");  
  state.add("Maine");  
  state.add("Maryland");
  state.add("Massachusetts");  
  state.add("Michigan");
  state.add("Minnesota");
  state.add("Mississippi");
  state.add("Missouri");
  state.add("Montana");  
  state.add("Nebraska");  
  state.add("Nevada");  
  state.add("New Hampshire");  
  state.add("New Jersey");
  state.add("New Mexico");
  state.add("New York");  
  state.add("North Carolina");
  state.add("North Dakota");  
  state.add("Ohio");
  state.add("Oklahoma");
  state.add("Oregon");  
  state.add("Pennsylvania");
  state.add("Rhode Island");  
  state.add("South Carolina");  
  state.add("South Dakota");
  state.add("Tennessee");  
  state.add("Texas");  
  state.add("Utah");
  state.add("Vermont");  
  state.add("Virginia");  
  state.add("Washington");  
  state.add("West Virginia");
  state.add("Wisconsin");  
  state.add("Wyoming");
  return SUCCESS;
  }
  public List getState(){
  return state;
  }
}



it creates a autocompleter list with the name of U.S. states.


autocompleter.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Struts 2 Autocompleter Example!</title>
  <s:head theme="ajax" />
  </head>
  <body>
  <h1>Struts 2 Autocompleter Example!</h1>
  <s:label name="stateName" value="Select State Name:" />
  <s:autocompleter theme="simple" list="state" name="StateName"/>
  </body>
</html>

Output of the autocompleter.jsp :




When you enter the 'c' letter in the textbox. You get the following names:


Popular posts from this blog

Struts2 - Radio Tag (Form Tags)

In this section, we are going to describe the radio tag. The radio tag is a UI tag that renders a radio button input field. Add the following code snippet into the struts.xml file. struts.xml <action name="radioTag" class="net.struts2.checkboxlistTag">   <result>/pages/uiTags/radioTag.jsp</result> </action> Create an action class with two lists as shown below: checkboxlistTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class checkboxlistTag extends ActionSupport{      private List fruits;   private List animals;     public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Apple");   fruits.add("Mango");   fruits.add("Orange");   fruits.add("Pine Apple");  ...

Struts2 - Combobox Tag (Form Tags)

The struts2 combobox is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box functionality. You can place text in the INPUT control by using the SELECT control or type it in directly in the text field. Add the following code snippet into the struts.xml file. struts.xml <action name="comboboxTag" class="net.struts2.comboboxTag">    <result>/pages/uiTags/comboboxTag.jsp</result> </action> Create "comboboxTag " action class and a list in the class and populate it with various items as shown in the class. comboboxTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class comboboxTag extends ActionSupport{      private List fruits;   public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Appl...

Global Action Forwards and Action Mappings

What is an action forward? A action forward can be used to forward to a jsp or action mapping. There are two different action forwards. The global action forward and the local action forward. You can access a global action forward on each jsp or action class. A local action forward can only be accessed by the assigned action class. What is a action mapping? The action mapping is the heart of struts. It managed all actions between the application and the user. You can define which action will be executed by creating a action mapping. The diagram show you, how the application server manage the request of the index.jsp or a non existing action mapping. In the first step we create a new action mapping. Open the struts-config.xml, you will find it in the folder WebRoot/WEB-INF. Right click in the outline window on action-mapping. Choose Use Case default and Action Type Forward. The Forward Path is the welcome page /jsp/index.jsp In the second step you create a global act...