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");

  animals = new ArrayList();
  animals.add("Dog");
  animals.add("Elephant");
  animals.add("Ox");
  animals.add("Fox");
  return SUCCESS;

  }

  public List getFruits(){
  return fruits;
  }

  public List getAnimals(){
  return animals;
  }
}


Create a jsp using the tag <s:radio>.It renders a radio button input field.

radioTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>Radio Tag Example!</title> 
 <link href="<s:url value="/css/main.css"/>" 
rel="stylesheet" type="text/css"/>  
  </head>
  <body>
<h1><span style="background-color: #FFFFcc">Radio Tag Example!
</span></h1>
<s:form>
 <s:radio label="Fruits" name="fruitsname" list="fruits"/>
 <s:radio label="Animals" name="animalsname" list="animals"/>
 </s:form>
 </body>
</html>


Output of the radioTag.jsp:

Struts2 - Optgroup Tag (Form Tags)

In this section, we are going to describe the optgroup tag. The optgroup tag is a UI tag that creates an optgroup component which needs to reside within a select tag <s:select>.

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

struts.xml
<action name="optgroupTag">
   <result>/pages/uiTags/optgroupTag.jsp</result>
</action>

Create a jsp using the tag <s:optgroup> within the <s:select> tag. It creates an optgroup component. This tag contains few parameters:
The label parameter sets the label attribute In our case we have set it to "Hardware" and "Software".


optgroupTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>Optgroup Tag Example!</title> 
  </head>
  <body>
 <h1><span style="background-color: #FFFFcc">Optgroup Tag Example!
</span></h>
 <s:form>
 <s:select label="Please Select"
  name="select"
 list="%{#{'PROGRAMMING':'Programming', 'DATABASE':'DataBase',
   'WEBAPPLICATION':'WebApplication'}}">
  <s:optgroup label="Hardware"
  list="%{#{'CPU':'Centeral Processing Unit','MOUSE':'Mouse',
    'KEYBOARD':'Keyboard'}}" />
  <s:optgroup label="Software"
  list="%{#{'SYSTEM SOFTWARE':'System Software','APPLICATION 
  SOFTWARE':'Application Software'}}" />
  </s:select>
  </s:form>
  </body>
</html>

Output of the optgroupTag.jsp:


Struts2 - Optiontransferselect Tag (Form Tags)

In this section, we are going to describe the Optiontransferselect tag. The Optiontransferselect tag is a UI tag that creates an option transfer select component. There are two <select ...> tags with buttons in the middle of them, which allows options in each of the <select ...> to be moved between them. It auto-selects all its elements upon its containing form submission.

NOTE: The id and doubleId parameters are not needed to supply as they will get generated when the optiontransferselect tag is being used in a form tag. The generated id and doubleId will be <form_id>_<optiontransferselect_doubleName> and <form_id>_<optiontransferselect_doubleName> respectively.

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

struts.xml
<action name="optiontransferselectTag">
  <result>/pages/uiTags/optiontransferselectTag.jsp</result>
</action>


Create a jsp using the tag <s:optiontransferselect> that creates an option transfer select component. This tag contains various parameters:

The label parameter sets label expression used for rendering an element specific label. In our case we have set it to "Employee Records"
The name parameter sets the name for the element.  In our case we have set it to "leftSideEmployeeRecords"
The leftTitle parameter sets the left title. In our case we have set it to "Struts2"
The rightTitle parameter sets the right title. In our case we have set it to "JavaJazzUp"
The headerKey sets the header key of the given list. It must not be empty. In our case we have set it to:"headerKey"
The headerValue sets the header value of the given list. In our case we have set it to:"--- Please Select ---"
The doubleName sets the name for complete component. In our case we have set it to:"rightSideEmployeeRecords"
The doubleHeaderKey sets the header key for the second list. In our case we have set it to:"doubleHeaderKey"
The doubleHeaderValue sets the  header value for the second list. In our case we have set it to:"--- Please Select ---"

optiontransferselectTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>Optiontransferselect Tag Example!</title>
 <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>  
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Optiontransferselect 
   Tag Example!</span></h>
  <s:form>
  <s:optiontransferselect 
  label="Employee Records"
  name="leftSideEmployeeRecords"
  leftTitle="Struts2"
  rightTitle="JavaJazzUp"
  list="{'Deepak Kumar', 'Sushil Kumar','Vinod Kumar','Deepak Monthy',
  'Deepak Mihanti', 'Sushil Kumar', 'Ravi Kant Kumar'}"
  headerKey="headerKey"
  headerValue="--- Please Select ---"
  doubleName="rightSideEmployeeRecords"
  doubleList="{'Amar Deep Patel', 'Amit Kumar','Chandan Kumar', 
   'Noor Kumar','Tammana Kumari'}"
  doubleHeaderKey="doubleHeaderKey"
  doubleHeaderValue="--- Please Select ---" />
  </s:form>
  </body>
</html>


Output of the optiontransferselectTag.jsp:

Struts2 - Password Tag (Form Tags)

In this section, we are going to describe the password tag. The password tag is a UI tag that renders an HTML input tag of type password.

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

struts.xml
<action name="passwordTag">
<result>/pages/uiTags/passwordTag.jsp</result> </action>

Create a jsp using the tag <s:password>. It renders an HTML input tag of type password.

passwordTag.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>Password Tag Example!</title> 
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Password Tag Example!
</span></h1>
 <s:form>
 <s:password label="Enter Password" name="password" size="10" maxlength="8" />
  </s:form>
  </body>
</html>

Output of the passwordTag.jsp:

Struts2 - Label Tag (Form Tags)

In this section, we are going to describe the label tag. The label tag is a UI tag that is used to render an HTML LABEL that allows us to output label:name type of combinations that has the same format treatment as the rest of UI controls.

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

struts.xml
<action name="labelTag">
   <result>/pages/uiTags/labelTag.jsp</result>
</action>


Create a jsp using the tag <s:label>. It renders an HTML LABEL that allows us to output <s: label name=" " value=" " /> combination that has the same format treatment as the rest of UI controls.

labelTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>Label Tag Example!</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Label Tag Example
</span></h1>
  <s:form>
  <s:label name="name" value= "Name" />
  <s:label name="roll" value= "Roll" />
  <s:label name="address" value= "Address: " />
  </s:form>
  </body>
</html>


Output of the labelTag.jsp:

Struts2 - Doubleselect Tag (Form Tags)

In this section, we are going to describe the doubleselect tag. The doubleselect tag is a UI tag that renders two HTML select elements with second one changing displayed values depending on selected entry of first one.

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

struts.xml
<action name="doubleselectTag">
  <result>/pages/uiTags/doubleselectTag.jsp</result>
</action>

Create a jsp using the tag <s:doubleselect> that renders two HTML select elements with second one changing displayed values depending on selected entry of first one. This tag contains various parameters:

The headerKey parameter sets the header key of the second list. Must not be empty. In our case we have set it to"1"

The headerValue parameter sets the header value of the second list. In our case we have set it to "--- Please Select ---"

The doubleName parameter sets the name for complete component. In our case we have set it as : doubleName="dishes"

The doubleList sets the second iterable source to populate from. In our case we have set it as :

doubleList="top == 'Color' ? {'Black','Green','White', 'Yellow','Red','Pink'} : { 'Apple','Banana','Grapes','Mango'}"


doubleselectTag.jsp

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

<html>
  <head>
  <title>Doubleselect Tag Example!</title>
  </head>
  <body>
<h1><span style="background-color: #FFFFcc">Doubleselect Tag Example!
</span></h1>
  <s:form>
  <s:doubleselect label="Select Item"  
  headerValue="--- Please Select ---"
  headerKey="1" list="{'Color','Fruits'}" 
  doubleName="dishes" 
  doubleList="top == 'Color' ? {'Black','Green','White',
 'Yellow','Red','Pink'} : { 'Apple','Banana','Grapes','Mango'}"/>
  </s:form>
  </body>
</html>

Output of the doubleselectTag.jsp:

Struts2 - File Tag (Form Tags)

In this section, we are going to describe the file tag. The file tag is a UI tag that renders an HTML file input element achieved through browsing.

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

struts.xml
<action name="fileTag">
  <result>/pages/uiTags/fileTag.jsp</result>
</action>

Create a jsp using the tag . It renders an HTML file input element.
The parameter name is used to set a name for element which we have used as name="uploadFile".

and the parameter accept is the HTML accept attribute that indicates the accepted file mime types which we have used as accept="text/*".

fileTag.jsp

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

<html>
  <head>
  <title>File Tag Example!</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">File Tag Example!
</span></h1>
  <b>File Name</b>
  <s:form>
  <s:file name="uploadFile" accept="text/*" />
  </s:form>
  </body>
</html>

Output of the fileTag.jsp


On clicking the "Browse.." button. You will see a window that can be used to browse a file to upload.

Struts2 - Form Tag

In this section, we are going to describe the form tag. The form tag is a UI tag that renders HTML an input form. The remote form allows the form to be submitted without the page being refreshed. The results from the form can be inserted into any HTML element on the page.

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

struts.xml
<action name="formTag">
<result>/pages/uiTags/formTag.jsp</result> </action>

Create a jsp using the tag <s:form>.It renders HTML as an input form

formTag.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>Form Tag Example!</title>
  <link href="<s:url value="/css/main.css"/>" 
  rel="stylesheet" type="text/css"/>  
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Form Tag Example
</span></h1>
  <s:form>
  <s:textfield name="username" label="Login name"/>
  <s:password name="password" label="Password"/>
 <s:submit value="Login" align="center"/>
  </s:form>
  </body>
</html>



Output of the formTag.jsp: