Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Struts
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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></h>
  <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 :

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

7 comments so far (post your own) View All Comments Latest 10 Comments:

How to write logic tag in struts2.0

can you help me plz

Posted by prasad on Wednesday, 09.24.08 @ 01:21am | #80644

Is this done in struts.xml. If so can I see this artifact too in context of this example

Posted by Jeremy on Tuesday, 09.23.08 @ 22:54pm | #80642

There is actually a problem with this tag in Struts 2 !! If you give a namespace to appropriate package and containing form, select tag doesn't recognize list parameter anymore.

Posted by Miki on Wednesday, 04.16.08 @ 00:59am | #56639

Good job showing how to add the select box. Now how do we use the data that gets submitted with the form? I have uses the month example in a page with an action that has a getter and setter for the field I put in the name attribute of the select tag. I have tried declaring it as a String and an Integer but no matter what it ends up as null in the action after submitting the form. Can you provide an example of how the action should handle the select tag?

Thanks!

Posted by Dave on Wednesday, 02.13.08 @ 22:38pm | #48180

When I use the struts2 select tag with a static map like:

<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'}"
/>

on Weblogic Server 10.x I get a JSP compilation error on the semicolon (:). Weblogic seems to complain about the tag format for the list:
list="#{'01':'January','02':'February'}

Any idea why this is happening? Could it be a clash with the Struts1 jars?

Posted by Seth on Monday, 12.17.07 @ 23:04pm | #42749

Hi
i am getting only title ie "select tag Examples". i am not getting the other result can any help me over here

Posted by vijay on Tuesday, 11.27.07 @ 17:29pm | #40713

Hello,
Is there any ways for us to set selected option based on variables set by an action who render the jsp for success result?

like on your example, how can we automatically set the selected option of date, to option number 5 maybe.

Posted by First on Monday, 08.6.07 @ 13:23pm | #22746

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.