Struts 2 <s:checkboxlist> multiple check boxes example

In Struts 2 , you can use the <s:checkboxlist> tag to create multiple check boxes with a same name. The only concern is how to hold the multiple checked values in a variable? For example,

public List<String> getColors() {
	colors = new ArrayList<String>();
	colors.add("red");
	colors.add("yellow");
	colors.add("blue");
	colors.add("green");
	return colors;
}
<s:checkboxlist label="What's your favor color" list="colors" 
name="yourColor" value="defaultColor" />

A multiple check boxes with “red”, “yellow”, “blue” and “green” options. If multiple options are checked, you can store it via a String object.

For example, If “red” and “yellow” options are checked, the checked values will combine with a comma, yourColor = “red,yellow”.

private String yourColor;
 
public void setYourColor(String yourColor) {
	this.yourColor = yourColor;
}
Read this article about how to set the default value for multiple check boxes.

Struts 2 <s:checkboxlist> example

A full Struts 2 example to create multiple check boxes with a same name via <s:checkboxlist>, stored the checked values and display it in another page.

1. Action

Action class to generate and hold the multiple check boxes values.
CheckBoxListAction.java

package com.mkyong.common.action;
 
import java.util.ArrayList;
import java.util.List;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class CheckBoxListAction extends ActionSupport{
 
	private List<String> colors;
 
	private String yourColor;
 
	public String getYourColor() {
		return yourColor;
	}
 
	public void setYourColor(String yourColor) {
		this.yourColor = yourColor;
	}
 
	public CheckBoxListAction(){
		colors = new ArrayList<String>();
		colors.add("red");
		colors.add("yellow");
		colors.add("blue");
		colors.add("green");
	}
 
	public String[] getDefaultColor(){
		return new String [] {"red", "green"};
	}
 
	public List<String> getColors() {
		return colors;
	}
 
	public void setColors(List<String> colors) {
		this.colors = colors;
	}
 
	public String execute() {
		return SUCCESS;
	}
 
	public String display() {
		return NONE;
	}
}

2. Result page

Render the multiple check boxes via “s:checkboxlist” tag.
checkBoxlist.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 multiple check boxes example</h1>
 
<s:form action="resultAction" namespace="/">
 
<h2>
	<s:checkboxlist label="What's your favor color" list="colors" 
	   name="yourColor" value="defaultColor" />
</h2> 
 
<s:submit value="submit" name="submit" />
 
</s:form>
 
</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 
<body>
<h1>Struts 2 multiple check boxes example</h1>
 
<h2>
  Favor colors : <s:property value="yourColor"/>
</h2> 
 
</body>
</html>

3. struts.xml

Link all together ~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
 <constant name="struts.devMode" value="true" />
 
<package name="default" namespace="/" extends="struts-default">
 
   <action name="checkBoxListAction" 
         class="com.mkyong.common.action.CheckBoxListAction" method="display">
	<result name="none">pages/checkBoxlist.jsp</result>
   </action>
 
   <action name="resultAction" class="com.mkyong.common.action.CheckBoxListAction">
	<result name="success">pages/result.jsp</result>
   </action>
  </package>
 
</struts>

5. Demo

http://localhost:8080/Struts2Example/checkBoxListAction.action

Struts 2 checkboxlist example

http://localhost:8080/Struts2Example/resultAction.action

Struts 2 checkboxlist example

Reference

  1. Struts 2 checkboxlist documentation
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: stream movies()

  • Pingback: Blue Coaster33()

  • http://www.ihatebugs.net Justin

    Hi rodney,

    For me, I will iterate the list, adding one
    checkbox per row.

    Hope it helps :)

  • Rodney

    Hello mkyong, congratulations to your post.
    I whould like show the options not one line, but each checkbox option in a line.
    Example:
    [] checkbox_1
    [] checkbox_2
    [] checkbox_3

    How I could make that it?

    Success…

  • http://www.ihatebugs.net Justin

    Hi nitish,

    It means that your checkboxlist color is null.
    Please check your action file, where the content
    population is done.
    And also check your getter and setter method.

    Hope it helps.

    Cheers,
    Justin

  • Nitish Talekar

    I got same error while using checkbox and also for radio buttons.
    Error is —
    exception

    org.apache.jasper.JasperException: tag ‘checkboxlist’, field ‘list’, name ‘yourcolor': The requested list key ‘colors’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
    root cause

    tag ‘checkboxlist’, field ‘list’, name ‘yourcolor': The requested list key ‘colors’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
    org.apache.struts2.components.Component.fieldError(Component.java:240)
    org.apache.struts2.components.Component.findValue(Component.java:333)
    org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
    org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:864)
    org.apache.struts2.components.UIBean.end(UIBean.java:516)
    org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    org.apache.jsp.Customer_jsp._jspx_meth_s_005fcheckboxlist_005f0(Customer_jsp.java:313)
    org.apache.jsp.Customer_jsp._jspx_meth_s_005fform_005f0(Customer_jsp.java:155)
    org.apache.jsp.Customer_jsp._jspService(Customer_jsp.java:88)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.21 logs.

    can any one give me solution for this ???

  • http://www.ihatebugs.net Justin

    Oops, seem like my codes that I typed, not shown at the.comments.

    Will post at my site :)

  • http://www.ihatebugs.net Justin

    Great post, mk :)

    Manage to fix it when used for multiple checkboxes.

    This is my code:

    Hope can benefit someone out there :)

  • RAM

    Good Post But you missed listKey and listValue options that wil bother to newbees.

  • naveen garg

    error:

    org.apache.jasper.JasperException: tag ‘checkboxlist’, field ‘list’, name ‘yourColors': The requested list key ‘colors’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:527)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:73)

    root cause

    tag ‘checkboxlist’, field ‘list’, name ‘yourColors': The requested list key ‘colors’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
    org.apache.struts2.components.Component.fieldError(Component.java:230)
    org.apache.struts2.components.Component.findValue(Component.java:351)
    org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
    org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:856)
    org.apache.struts2.components.UIBean.end(UIBean.java:510)
    org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    org.apache.jsp.checkbox_jsp._jspx_meth_s_005fcheckboxlist_005f0(checkbox_jsp.java:148)
    org.apache.jsp.checkbox_jsp._jspx_meth_s_005fform_005f0(checkbox_jsp.java:109)
    org.apache.jsp.checkbox_jsp._jspService(checkbox_jsp.java:72)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:73)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.24 logs.

  • naveen garg

    every example display same error whats problem please solve ?

    • Vivek S

      just change the name “yourColors” to all lowercase “yourcolors” this will solve your issues.

      • saumya

        Its still not working..showing the same error

      • Ram

        i changed the yourColors to yourcolors,also it not working