16_8 - checkbox Interceptor

Introduction:

  • The chapter will explain the interceptor “checkbox” in Struts 2 with an example program.

Execute and Wait Interceptor:

  • The checkbox interceptor is used to handle checkboxes in Struts 2.
  • Normally, checkboxes are used for multiple selections of menu items in HTML and radio buttons are used for single selection (options). For example, for subject selection we will use checkboxes while for selecting gender we will use radio button.
  • Multiple selections refer to more than one selection and no selection also. But there are some applications which may require determining the further actions based on the selection of checkbox. The main use of checkbox interceptor is to facilitate checkbox selection.
  • That is, the next page to be displayed is dependent on selection or de-selection of a checkbox. Such events are handled by checkbox interceptor.
  • To use Checkbox interceptor, we have to first create a checkbox in our web application. The syntax of declaring checkbox is as follows:

     <s: checkbox name = "Name of Checkbox" label = "Label text of checkbox" fieldValue = “true / false” />

  • In our web application, we will ask the user to remember the credentials so that the username and password, re-enter will not be required for every time when the application launches.
  • So the first.jsp can be written as:
// first.jsp

<%-- 
    Document   : first
    Created on : Nov 20, 2014, 12:34:17 PM
    Author     : Admin
--%>

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title> Execute and Wait Demo - Employee Login Application </title>
    </head>
    <body>
        <h1>
             Employee Login System
        </h1>
        <hr/>
        <s:form action = "click">
            <s:label value = "Enter username : "/> <s:textfield name = "name"/>
            <br/>
            <s:label value = "Enter password : "/> <s:textfield name = "pwd"/>
            <br/>
            <s:checkbox name = "myCheckbox" label = "Remember Password"/>
            <br/>
            <s:submit/>  
         </s:form>            
    </body>
</html>         
  • Now the next page will simply print users name and the checkbox value (true or false based on user’s selection) on successful login.
  • So, next.jsp can be written as follows:
// next.jsp

<%-- 
    Document   : next
    Created on : Nov 20, 2014, 12:50:20 PM
    Author     : Admin
--%>

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>
<%@page  language = "java" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title> Employee Page </title>
    </head>
    <body>
      <h1> Login success </h1>
        <hr/>
      <h2>
              Welcome <s:property value = "name"/>.. 
              Remember Password : <s:property value = "myCheckbox"/>
      </h2>
   </body>
</html>
  • The action class will only contain getter and setter methods of all the variables used in our first.jsp.  
  • The checkbox will be either selected or de-selected. So the data type of storing its information is “Boolean”. Also the setter method and getter method for Boolean variables can be given as follows:
public void setBooleanVariable (boolean var_name)
    {
       this. var_name = var_name;
    }
public boolean isBooleanVariable()
    {
            return var_name;
    }
  • So Myaciton1.java will be coded as:
// Myaction_1.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package action_class;

import com.opensymphony.xwork2.ActionContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.CookiesAware;

/**
 *
 * @author Admin
 */
public class Myaction_1 extends ActionSupport
{
    private String name, pwd;
    private boolean myCheckbox;

    public void setMyCheckbox (boolean myCheckbox)
    {
        this. myCheckbox = myCheckbox;
    }

    public boolean isMyCheckbox ()
    {
       return myCheckbox;
    }
    public void setName(String name)
    {
       this.name = name;
    }
    public void setPwd(String pwd)
    {
      this.pwd = pwd;
    }
    public String getName()
    {
       return name;
    }
    public String getPwd()
    {
       return pwd;
    }
// Method to save users credentials on users web browser..

     public void save_info()
        {
                Cookie ck1 = new Cookie(“username” , name);
                Cookie ck2 = new Cookie(“password” , pwd);
                HttpServletResponse res;
                res.addCookie(ck1);
                res.addCookie(ck2);
        }
  
      public String execute() 
        {
            if(name == “abc” && pwd == “abc”)
            {
                  save_info();
                  return SUCCESS;
            }
            else
            {
                  return “failure”;
            }            
        }
}
  • Now to combine our JSP’s with action class, we need to configure our struts.xml.
  • No interceptor reference is used to declare here (optional). But to use checkbox interceptor , following syntax is used under action element:

           <interceptor-ref name = "checkbox"/>

// 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 = "click" class = "action_class.Myaction_1">
                  <!-- optional interceptor decalartions -- >

             <interceptor-ref name = "checkbox"/>
              <result name = "success"> next.jsp </result>
              <result name = "none"> first.jsp </result>
        </action>
    </package>
</struts>
  • The web.xml can be given as:
// web.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name> struts2 </filter-name>
        <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
    </filter>
    <filter-mapping>   
        <filter-name> struts2 </filter-name>
        <url-pattern> /* </url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file> jsp/first.jsp </welcome-file>
    </welcome-file-list>
</web-app>

   Our application will run as follows:

Figure: first run of application

                                     Figure: first run of application

 

Figure: entering credentials of user. In first run

 

Figure: entering credentials of user. In first run, we are not selecting the checkbox. So no credentials will be saved in user’s browser. 

Figure: Output of application without selecting checkbox

Figure: Output of application without selecting checkbox

Figure: entering credentials of user.

Figure: entering credentials of user. In second run, we will select the checkbox. So user’s information will be saved in the web browser. 

Figure: Output of application with selected checkbox

Figure: Output of application with selected checkbox

Like us on Facebook