Magazine
 
Struts2 Tags

checkboxTag.jsp

<%@ taglib prefix=”s” uri=”/struts-tags” %>
<html>
<head>
<title>Checkbox (Form Tag) Tag
Example</title>
</head>
<body>
<h2>Checkbox Tag Example</h2>
<b>Sex</b><br>
<s:checkbox label=”Male” name=”male”
value=”true” /><br>
<s:checkbox label=”Female”
name=”male” />
</body>
</html>

Output of the checkboxTag.jsp:

3. Checkboxlist Tag (Form Tag) Example

The checkboxlist tag is a UI tag that creates a series of checkboxes from a list. Setup is like <s:select /> or <s:radio />, but creates checkbox tags.

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

<action name=”checkboxlistTag”
class=”net.javajazzup.checkboxlistTag”>
<result>/pages/formTags/
checkboxlistTag.jsp</result>
</action>

 

 

Create two lists in the action class and populate them with various items as shown in the “checkboxlistTag” class.

checkboxlistTag.java

package net.javajazzup;
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:checkboxlist>

<s:checkboxlist name=”Fruits-name”
list=”fruits” /> prints a checboxlist with name
Fruits and Creates a series of checkboxes from
fruits list of the action class “checkboxlistTag”.
<s:checkboxlist name=”Animals-name”
list=”animals” /> prints a checboxlist with name

     
Feb 2008 | Java Jazz Up | 39
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 ,

Download PDF