Saturday, 24 October 2015

Struts2 : checkbox example

Checkbox tag renders HTML check box. If checkbox is selected, then the value sent in request parameter in “on”, if checkbox didn’t select, then no value is sent in request parameters.

For example
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <s:form action="formAction" method="post">
    <s:label> Simple form example</s:label>
    <s:textfield name="userName" label="user name : " />
    <s:password name="password" label="password : " />
    <s:hidden name="code" value="1" />
    <s:textarea name="description" rows="10" cols="50"
      label="Tell us about yourself" />

    <s:label> Your Hobbies </s:label>
    <s:checkbox name="movies" label="Movies" />
    <s:checkbox name="cricket" label="Cricket" />
    <s:checkbox name="coding" label="Coding" value="true" disabled="true" />
    <s:submit />

  </s:form>
</body>
</html>


FormAction.java
package strutstutorial.actions;

public class FormAction {
  private String userName;
  private String password;
  private int code;
  private String description;
  private boolean movies;
  private boolean cricket;
  private boolean coding;

  public String execute() {
    System.out.println(this);
    return "success";

  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public boolean getMovies() {
    return movies;
  }

  public void setMovies(boolean movies) {
    this.movies = movies;
  }

  public boolean getCricket() {
    return cricket;
  }

  public void setCricket(boolean cricket) {
    this.cricket = cricket;
  }

  public boolean getCoding() {
    return coding;
  }

  public void setCoding(boolean coding) {
    this.coding = coding;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("FormAction [userName=").append(userName)
        .append(", password=").append(password).append(", code=")
        .append(code).append(", description=").append(description)
        .append(", movies=").append(movies).append(", cricket=")
        .append(cricket).append(", coding=").append(coding).append("]");
    return builder.toString();
  }

}


struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <package name="default" extends="struts-default">   
    <action name="formAction" class="strutstutorial.actions.FormAction">
      <result name="success">/success.jsp</result>
    </action>

  </package>
</struts>


For some input, where movies is not selected and cricket is selected, following output comes into console.

FormAction [userName=Krishna, password=, code=1, description=I am a software Engineer, movies=false, cricket=true, coding=false]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment