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("Apple");
  fruits.add("Mango");
  fruits.add("Orange");
  fruits.add("Pine Apple");
  return SUCCESS;

  }

  public List getFruits(){
  return fruits;
  }
}


Create a jsp using the combobox tag


The first struts2 combobox tag prints a combobox with name Color name and an HTML INPUT of type text and HTML SELECT grouped together created using the list.

The second
struts2 combobox tag prints a combobox with name Fruits name and an HTML INPUT of type text and HTML SELECT grouped together created using the "fruits" list of the action class.

comboboxTag.jsp

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

<html>
  <head> 
  <title>Combobox  (Form Tag) Tag Example!</title>
  <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Combobox 
   Tag Example!</span></h1>
  <s:form>
  <s:combobox label="Colors Name" name="colorNames" 
   headerValue="--- Please Select ---"
  headerKey="1" list="{'Black','Green','White','Yellow',
  'Red','Pink'}" /><br>
  
  <!-- Use array list --><br>
  <s:combobox label="Fruits Name" name="fruitsNames"
    headerValue="--- Please Select ---"
  headerKey="1" list="fruits" />
  </s:form>
  </body>
</html>

Output of the struts2 comboboxTag.jsp:


struts2 combobox

2 comments:

  1. Thanks again for the example .... Terrific .....

    ReplyDelete
  2. You must take part in a contest for top-of-the-line blogs on the web. I will recommend this website! online casino games

    ReplyDelete