16.6 - Struts param Interceptor

Introduction:

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

parameter Interceptor:

  • The parameter interceptor is used to give static values to variables rather than dynamic values. These values are given in first jsp page and passed in URL to the next page for further use. The only use of “param” interceptor is to provide static values.
  • The syntax for giving param to JSP page is:

<s:url action = "Name of Action" id = "ID of URL (generally its “url”) ">

<s:param name = "Name of Param1" value = “value of param1”> Value of Param1 </s:param>

</s:url>

  • So the first.jsp can be coded 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> Param Demo - Library Application </title>
    </head>
    <body>
    <h1>  Param Demo - Library Application
        </h1>
        <hr/>
        <s:url action = "click" id = "url">
            <s:param name = "bid"> B001 </s:param>    
            <s:param name = "sname"> Java </s:param>    
            <s:param name = "bname"> Daniel Liang </s:param>    
            <s:param name = "bprice"> 100.03 </s:param>
        </s:url>
            <h3>
               <s:a href = "%{url}"> Param demo - Book Details </s:a>
        </h3>
    </body>
</html>
  • The next.jsp will display the values of fields from first.jsp.
  • 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> Book Details Page </title>
    </head>
    <body>
       <h1> Book Details are: </h1>
          <hr/>
       <h4>
         <s:label value = "Book ID : "/> <s:property value = "bid"/>
         <br/>
         <br/>
         <s:label value = "Subject Name : "/> <s:property value = "sname"/>
         <br/><br/>
         <s:label value = "Book Name : "/> <s:property value = "bname"/>
         <br/><br/>
         <s:label value = "Book Price : "/> <s:property value = "bprice"/>
         <br/><br/>
      </h4> 

   </body>
</html>
  • The action class will only contain getter setter methods of all the variables used in our first.jsp. 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 com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
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 double bprice;
        private String bid,sname,bname;
        public void setBid(String bid)
        {
            this.bid = bid;
        }
        public void setSname(String sname)
        {
            this.sname = sname;
        }
        public void setBname(String bname)
        {
            this.bname = bname;
        }
        public void setBprice(double bprice)
        {
            this.bprice = bprice;
        }
        public String getBid()
        {
            return bid;
        }
        public String getBname()
        {
            return bname;
        }
        public String getSname()
        {
            return sname;
        }
        public double getBprice()
        {
            return bprice;
        }        
        public String execute()
        {
            ActionContext con = ActionContext.getContext();
            HttpServletRequest req = (HttpServletRequest) con.get(ServletActionContext.HTTP_REQUEST);
            setBid(req.getParameter("bid"));
            setSname(req.getParameter("sname"));
            setBname(req.getParameter("bname"));
            setBprice(Double.parseDouble(req.getParameter("bprice")));
            return SUCCESS;
        }
} 
  • Now as we are not giving the input values in the first form dynamically, we are required to retrieve that from the URL by using HttpServletRequest object via the current Action context. This can be done by:
    • Getting the current action context

ActionContext con = ActionContext.getContext();

  • Creating the HttpServletRequest object and referring it to the current context.

HttpServletRequest req = (HttpServletRequest) con.get(ServletActionContext.HTTP_REQUEST);

  • Request is retrieved by request.getParameter(“field name”) method.
  • Now to combine our JSP’s with action class, we need to configure our struts.xml.
//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">
                    <result name = "success"> next.jsp </result>
        </action>
    </package>
</struts>
  • And 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>
  • So the application will run as follows:

 

Figure: first.jsp of web application

Figure: first.jsp of web application

Figure: next.jsp page is executed which will display the param values passed in first.jsp

Figure: next.jsp page is executed which will display the param values passed in first.jsp

 

 

 

 

Like us on Facebook