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="/">
 
<h4>
	<s:checkboxlist label="What's your favor color" list="colors" 
	   name="yourColor" value="defaultColor" />
</h4> 
 
<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>
 
<h4>
  Favor colors : <s:property value="yourColor"/>
</h4> 
 
</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 :
Founder of Mkyong.com, love Java and open source stuffs. Follow him on Twitter, or befriend him on Facebook or Google Plus.
Here are some of my recommended Books

Related Posts

Popular Posts