Struts 2 <s:checkboxlist> multiple check boxes example

Struts 2 <s:checkboxlist> multiple check boxes example

Previous Home Next

 

The Struts 2.0 framework provide checkboxlist to select multiple item in the list through the<s:checkboxlist></s:checkboxlist> tag. 
This tag provide the facility to get multiple element in the list .
The Struts 2 checkboxlist component provide the following <s:checkboxlist></s:checkboxlist> tag:
<s:checkboxlist label="Course Name" list="courses" name="course" value="defaultCourse"/>


 
Directory Structure of <s:checkboxlist> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<a href="checkBoxListAction">Check Box List Send Request</a>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="checkBoxListAction" class="org.r4r.CheckBoxListAction"method="display">
<result name="none">checkboxlist.jsp</result>
</action>
<action name="checkList" class="org.r4r.CheckBoxListAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

CheckBoxAction.java

package org.r4r;

import java.util.ArrayList;
import java.util.List;

public class CheckBoxListAction {
	private List<String> courses;
	private String course;
	
	public CheckBoxListAction(){
		courses=new ArrayList<String>();
		courses.add("MCA");
		courses.add("MBA");
		courses.add("BCA");
		courses.add("BBA");
		courses.add("PGDM");
	}
	public String execute(){
		return "success";
	}
	public String[] getDefaultCourse(){
		return new String [] {"B.Sc.", "M.Sc."};
	}
	public List<String> getCourses() {
		return courses;
	}
	public void setCourses(List<String> courses) {
		this.courses = courses;
	}
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
	}
	public String display() {
		return "none";
	}

}

checkboxlist.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<h2>Check Box List Example in Struts2 Framework</h2>
<s:form action="checkList">
<s:checkboxlist label="Course Name" list="courses"  name="course" value="defaultCourse"/>
<s:submit value="Submit" name="Submit"/>
</s:form>

success.jsp

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

<h2>Check Box List Example in Struts2 Framework</h2><br/>
My Course is:-<s:property value="course"/>

Output






Previous Home Next