JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Struts 2 CheckBox Tag Example

December 14, 2013 by Krishna Srinivasan Leave a Comment

Struts 2 s:checkbox tag is helpful for selcting the multiple options in a web page. The values for each field is stored as “true” or “false” based on the selection status of each field. The data type of check box filed is of boolean. The below example shows a very simple example with single check box in a page and taking the selected value to the next page.

1. Action Class

[code lang=”java”]
package javabeat.net.struts2;

public class Struts2UITagsAction{
private boolean checked;

public boolean isChecked() {
return checked;
}

public void setChecked(boolean checked) {
this.checked = checked;
}
public String execute(){
return "success";
}

}

[/code]

2. CheckBox Tag Example

CheckBox.jsp

[code lang=”xml”]
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<h1>JavaBeat – Struts 2 CheckBox Tag Demo</h1>
<s:form action="Result">
<s:checkbox name="checked" label="isChecked" />
<s:submit label="Submit" />
</s:form>

</body>
</html>
[/code]

Result.jsp

[code lang=”xml”]
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
<h1>JavaBeat – Struts 2 CheckBox Tag Demo</h1>
<h4>
Is Checked? : <s:property value="checked" /><br>
</h4>

</body>
</html>
[/code]

3. Struts.xml

[code lang=”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>
<constant name="struts.devMode" value="true" />
<package name="textfielddemo" extends="struts-default">
<action name="Result" class="javabeat.net.struts2.Struts2UITagsAction" >
<result name="success">Result.jsp</result>
</action>
</package>
</struts>
[/code]

4. CheckBox Tag Demo

If you access the application using URL http://localhost:8080/Struts2App/CheckBox.jsp, you would see the following output.

struts2 checkbox tag example input-screen

struts2 checkbox tag example output screen

Filed Under: Struts Tagged With: Struts 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved