Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Struts Struts2 Struts2uitags Select Tag (Form Tag) Example

Related Tutorials


 
 

Share on Google+Share on Google+

Select Tag (Form Tag) Example

Advertisement
In this section, we are going to describe the select tag. The select tag is a UI tag that is used to render an HTML input tag of type select.

Select Tag (Form Tag) Example

     

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

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

<action name="selectTag" class="net.roseindia.weekDay">
   <result>/pages/uiTags/selectTag.jsp</result>
</action>

Create an action class with a list populated with various items as shown below:
weekDay.java

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

public class weekDay extends ActionSupport{
  
  private List day;
  public String execute()throws Exception{
  day = new ArrayList();
  day.add("Sunday");
  day.add("Monday");
  day.add("Tuesday");
  day.add("Wednesday");
  day.add("Thursday");
  day.add("Friday");
  day.add("Saturday");
  return SUCCESS;

  }

  public List getDay(){
  return day;
  }
}

Create a jsp using the tag <s:select> that creates an HTML input tag of type select. This tag contains various parameters:

The label parameter sets the label expression used for rendering an element specific label. In our 1st case we have set it to "Select Day" 
The name parameter sets the name for the element.  In our 1st case we have set it to "daysname"
The headerKey sets key for first item in list. It must not be empty and wrongly specified. In both cases we have set it to:"1"
The headerValue
sets
the Value expression for the first item in the list. In both cases we have set it to:"--- Please Select ---"

selectTag.jsp 

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

<html>
  <head>
  <title>Select Tag Example</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Select Tag Example!</span></h1>
  <s:form>
  <s:select label="Select Day" 
  name="daysname" 
  headerKey="1"
  headerValue="-- Please Select --"
  list="day"
  />


  <s:select label="Select Month" 
  name="monthname" 
  headerKey="1"
  headerValue="-- Please Select --"
  list="#{'01':'January','02':'February','03':'March','04':'April',
'05':'May','06':'June','07':'July','08':'August','09':'September','10':
'October','11':'November','12':'December'}"

  />

  </s:form>
  </body>
</html>

Output of the selectTag.jsp :

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 19, 2008

Related Tutorials

Discuss: Select Tag (Form Tag) Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Phoenix
June 14, 2011
<s:select> tag how to set multiple default values

Hi all, I set a drop-down with <s:select multiple="true"...></select>,when I selected multiple options then submit , it will return to current page, but the drop-down can't be selected the multiple values , I want to know that how can make the drop-down's default multiple values are which I have chosen.Thank you so much.
Phoenix
June 14, 2011
how to set multiple default values for select tag

Hi all, I set a drop-down with <s:select multiple="true"...></select>,when I selected multiple options then submit , it will return to current page, but the drop-down can't be selected the multiple values , I want to know that how can make the drop-down's default multiple values are which I have chosen.Thank you so much.
ahlam
June 30, 2011
problem select ant textfield struts 2

I am starting in struts 2 and I want to create a Select control where I put the three choices: ouvrier ,frais directe et chef d'equipe if the user select "frais directe" I want that a textfield appears to writ "the position". I can not display the textfield when the choice of "frais directe" is made. code to create the Select control is as follows: <s: select list ="#{' 1 ':' chefEquipe ', '2', 'worker', '3 ',' FraiDirecte '} "headerKey =" -1 "headerValue =" select status "name =" status "label =" Status "> </ s: select> please help me I'm stuck thank you . salut je suis debutante en struts 2 et je veux créer un control Select dans lequel je mets les 3 choix suivant: ouvrier ,frais directe et chef d'equipe si l'utilisateur choisie frais directe alors un textfield apparait pour qu'il saisisse le poste . j'arrive pas à afficher le textfield une fois le choix de " frais direct" est fait. le code de création du control Select est le suivant: <s:select list="#{'1':'chefEquipe','2':'ouvrier','3':'FraiDirecte'}" headerKey="-1" headerValue="choisir statut" name="statut" label="Statut" ></s:select> aidez moi s'il vous plais je suis bloqué merci d'avance .
shri patrudkar
November 27, 2011
Struts 2 select tag difficulty

Hi, Can you tell me why for (below mentioned code of the same as above) case not needed to create ArrayList()like first select button? what # means in list="#{ here? Waiting for reply please do its necessary & urgent for me! Thanks! <s:select label="Select Month" name="monthname" headerKey="1" headerValue="-- Please Select --" list="#{'01':'January','02':'February','03':'March','04':'April', '05':'May','06':'June','07':'July','08':'August','09':'September','10': 'October','11':'November','12':'December'}" />
David
January 12, 2013
Delete option in the <s:select>

I have a question on how to do a thing. I have one that I <s:select> returns a list of results, and I wonder how I can remove one of the options I returns the database query. The condition I have completed the list, but I need the result. Not if there are any attributes within <s:select> me the option to remove it, or I have to do with javascript? Thank you very much for the help, I learned a lot with your tutorials. Regards, David
Bhagwan Singh
April 4, 2012
problem to get value from session in action class

I have set value in session in jsp page when i get value from session then it show error please give me answer Please give me answer with code Regards Bhagwan Singh
DMCA.com