Introduction:
- The chapter will explain the interceptor “alias” in Struts 2 with an example program.
alias Interceptor:
- “Alias” is a term used to define another name for existing variable or function name.
- In struts 2 “alias” interceptor is used for defining another name of the existing field variable name. Till now, we have used the same name of JSP (controls) field name and the variable name in getter setter methods of the action class. But an alias can be also used if respective mapping is provided.
- This mapping can be provided by alias interceptor in the configuration file.
- To use “alias” interceptor, create a folder “jsp” under source packages in NetBeans and create first.jsp as follows. The JSP accepts inputs like Student ID, Student Name, and Score of student in Test 1, Test 2 and Test 3. The application calculates the average score scored by a student in test series and displays it in the next JSP page.
// 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> Main Page </title> </head> <body> <s:form action = "click" method = "POST"> <h1> Student Score Management System </h1> <hr/> <s:label value = "Student ID"/> <s:textfield name = "sid"/> <s:label value = "Student Name"/> <s:textfield name = "sname"/> <s:label value = "Score of Test 1"/> <s:textfield name = "marks1"/> <s:label value = "Score of Test 2"/> <s:textfield name = "marks2"/> <s:label value = "Score of Test 3"/> <s:textfield name = "marks3"/> <s:submit value = "Get Score" name = "submit" /> </s:form> </body> </html>
- The next.jsp will gather the information from first.jsp, and will display: student id, student name and the average marks scored from all test series.
- As we are using alias here, make sure that in next.jsp, the variable names will not be same as the one used in first.jsp. And the mapping of variable names (which variable in first.jsp refers to which variations in next.jsp) will be explained in the configuration file.
- 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" %> <!DOCTYPE html> <html> <head> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> <title> Details Page </title> </head> <body> <h1> Details of student: </h1> <hr/> <h4> Student ID : <s:property value = "studentid"/> </h4> <h4> Student Name: <s:property value = "studentname"/> </h4> <h4> Student Average Score: <s:property value = "answer"/> </h4> </body> </html>
- The first.jsp will run as follows:
- Figure-1
- Now on click of “submit” button, the details should be retrieved from first.jsp and the calculation should display the result in next.jsp. The code for this should be written in “action” class. So create a package “action_class” under source packages directory and create a Java source file “myaction.java” inside “action_class” package.
- The getter setter methods of the newly named variables should be given in action class. That is, in this Java file, the “alias” names should be used for referring to any variable.
- In our example,
- sid is referred as studentid
- sname is referred as studentname
- marks1, marks2 and marks3 are referred as mks1, mks2, mks3 respectively.
- So myaciton.java will be coded as:
//myaction.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.ActionSupport; import java.io.*; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.apache.struts2.interceptor.ServletRequestAware; /** * * @author Admin */ public class myaction extends ActionSupport { private String studentid; private String studentname; private double mks1, mks2, mks3, answer; public double getMks1() { return mks1; } public double getMks2() { return mks2; } public double getMks3() { return mks3; } public void setMks1 (double mks1) { this.mks1 = mks1; } public void setMks2 (double mks2) { this.mks2 = mks2; } public void setMks3 (double mks3) { this.mks3 = mks3; } public String getStudentid() { return studentid; } public void setStudentid (String studentid) { this.studentid = studentid; } public String getStudentname() { return studentname; } public void setStudentname (String studentname) { this.studentname = studentname; } public double getAnswer() { return answer; } public void setAnswer (double answer) { this.answer = answer; } public String execute() { answer = (mks1 + mks2 + mks3) / 3; return SUCCESS; } }
- 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" namespace = "/" extends = "struts-default"> <action name = "click" class = "action_class.myaction"> <interceptor-ref name = “alias” /> <param name = "aliases"> # { 'sid' : 'studentid', 'sname' : 'studentname', 'marks1' : 'mks1', 'marks2' : 'mks2', 'marks3' : 'mks3' } </param> <result name="success">jsp/next.jsp</result> </action> </package> </struts>
- In struts.xml the interceptor is used by <interceptor-ref> element.
- The interceptor element is given as:
<interceptor-ref name = "alias">
- The parameters which are to be aliased are given is param tag with following syntax:
<param name = “aliases”>
# { ‘param_name_1’ : ‘aliased_name_1’ , ‘param_name_2’ : ‘aliased_name_2’ , …‘param_name_n’ : ‘aliased_name_n’}
</param>
- So, in our example, the param tag should be given as
<param name = "aliases">
# { 'sid' : 'studentid', 'sname' : 'studentname', 'marks1' : 'mks1', 'marks2' : 'mks2','marks3' : 'mks3' }
</param>
- And 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> </struts>
- So the application will run as follows: