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