• No results found

Interceptor Example 3

In document Struts2 Black Book (Page 167-171)

The third example is to describe the working of servlet-config and scoped-model-driven Interceptor. The servlet-config Interceptor sets action properties according to the different interfaces implemented by the action. Different interfaces, which are supported by this Interceptor, are as follows:

‰ ServletContextAware

‰ ServletRequestAware

‰ ServletResponseAware

‰ ParameterAware

‰ RequestAware

‰ SessionAware

‰ ApplicationAware

The Scoped-Model-Driven Interceptor works similar to Model-Driven, but the model is taken from the specified scope and again set into the same scope after being processed by the action.

The code files created for this example to work with are listed here:

‰ servlet_success.jsp

‰ scoped_student_info.jsp

‰ ServletAction.java

‰ ScopedModelAction.java

JSP Pages

The servlet_success.jsp page displays data from different scopes, like request and session. The details to be displayed are to be set by some action class, which is aware of Servlet parameters like request and session. We can access application, request, and session scoped attributes by using

<s:property/> tag using the following syntax:

<s:property value="#somescope.attributeName"/>

or

<s:property value="#somescope['attributeName']"/>

Here’s the code, given in Listing 4.38 for servlet_success.jsp page (you can find servlet_success.jsp file in Code\Chapter 4\struts2_i folder in CD):

Listing 4.38: servlet_success.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head><title><s:text name="app.title"/></title>

<link rel="stylesheet" href="mystyle.css" type="text/css" />

</head>

<body>

<h2>Request and Sesion Attributes..</h2>

Printing <b>Request</b> Attributes..

<br><br>The action class used : <b><s:property value="#request.action"/></b>

<br><br>The interceptor used: <b><s:property value="#request.interceptor"/> </b>

<br><br><br>Printing <b>Session</b> Attributes..

<br><br>User Name : <b><s:property value="#session.username"/></b>

<br><br>Company : <b><s:property value="#session.company"/></b>

<br><br><br>Printing Student Details from <b>Session</b> scope. <br><br>

Entollment No. : <s:property value="#session.student.rollno"/><br><br>

Student's Name : <s:property value="#session.student.name"/><br><br>

Student's Password : <s:property value="#session.student.password"/><br><br>

Course : <s:property value="#session.student.course"/><br><br>

City : <s:property value="#session.student.city"/><br><br>

<br><a href="scopedmodelAction.action">Invoke a ScopedModelDriven Action</a>

<br><br>

<a href="index.jsp">Back to Index</a>

</body>

</html>

The page also displays values of different properties of a Student class object stored in session scope.

This is obtained again by using the <s:property/> tag and setting the value to

#scope.attributeName.property.

The scoped_student_info.jsp displays the same Student detail as displayed in the servlet_success.jsp. But the page will be displayed through another action, which is capable of

processing a session scoped Student object. The process logic of different actions used in this example is described next.

Here’s the code, given in Listing 4.39, for scoped_student_info.jsp page (you can find scoped_student_info.jsp file in Code\Chapter 4\struts2_i folder in CD):

Listing 4.39: scoped_student_info.jsp

<%@ page language="java" import="com.kogent.action.Student"

pageEncoding="ISO-8859-1" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head><title><s:text name="app.title"/></title>

<link rel="stylesheet" href="mystyle.css" type="text/css"/>

</head>

<body>

<br>Printing Student Details from <b>Session</b> scope. <br><br>

Entollment No. : <s:property value="#session.student.rollno"/><br><br>

Student's Name : <s:property value="#session.student.name"/><br><br>

Student's Password : <s:property value="#session.student.password"/><br><br>

Course : <s:property value="#session.student.course"/><br><br>

City : <s:property value="#session.student.city"/><br><br>

<a href="index.jsp">Back to Index</a>

</body>

</html>

Actions – ServletAction and ScopedModelAction

One of the two actions created for this example is ServletAction. This action implements the ServletRequestAware, and SessionAware interface giving this action access to an object of HttpServletRequest and a session Map, which can store session scoped objects. The methods exposed by these interfaces are executed by the Servlet-Config Interceptor.

Here’s the code, given in Listing 4.40, for ServletAction.java (you can find ServletAction.java file in Code\Chapter 4\struts2_i\WEB-INF\src\com\kogent\action folder in CD):

Listing 4.40: ServletAction.java

package com.kogent.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class ServletAction extends ActionSupport implements ServletRequestAware, SessionAware{

HttpServletRequest request;

Map<String, Object> session_map;

public void setServletRequest(HttpServletRequest request) {

this.request=request;

}

public void setSession(Map map) {

this.session_map=map;

}

public String execute() throws Exception {

//Setting request attribute.

request.setAttribute("interceptor", "ServletConfigInterceptor");

request.setAttribute("action", "ServletAction");

Student student=new Student();

student.setRollno(101);

student.setName("Steve");

student.setPassword("steve123");

student.setCourse("B.Tech");

student.setCity("London");

//Setting session attributes.

session_map.put("username", "Prakash");

session_map.put("company", "Kogent Solutions Inc.");

session_map.put("student", student);

return SUCCESS;

} }

Another action used in this example is ScopedModelAction, which implements the com.opensymphony.xwork2.interceptor.ScopedModelDriven interface. The different methods of this interface, which needs to be implemented by this action class, are Object getModel(), String getScopeKey(), void setModel(Object obj), and void setScopeKey(String key). This action injects the model to be processed by the action, but this time the model is not populated by the input parameters, if any. Rather the model is picked from the scope defined, like request and session.

The Interceptor Scoped-Model-Driven is responsible for the execution of different methods implemented from ScopedModelDriven interface. The scope, key, and class of the scoped model is defined where the Interceptors are configured.

Here’s the code, given in Listing 4.41, for ScopedModelAction class (you can find ScopedModelAction.java file in Code\Chapter 4\struts2_i\WEB-INF\src\com\kogent\action folder in CD):

Listing 4.41: ScopedModelAction.java

package com.kogent.action;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.interceptor.ScopedModelDriven;

public class ScopedModelAction extends ActionSupport implements ScopedModelDriven{

private Student student;

private String key;

public Object getModel(){

System.out.println("getting model.");

return student;

}

public String getScopeKey() {

System.out.println("getting key.");

return key;

}

public void setModel(Object obj) {

System.out.println("setting model: "+obj.toString());

this.student=(Student)obj;

}

public void setScopeKey(String key) {

System.out.println("setting key: "+key);

this.key=key;

}

public String execute() throws Exception { System.out.println("executing");

student.setName(student.getName().toUpperCase());

student.setPassword(student.getPassword().toUpperCase());

student.setCourse(student.getCourse().toUpperCase());

student.setCity(student.getCity().toUpperCase());

return SUCCESS;

} }

This action class obtains the model from the scope defined and just changes its different properties to upper case, showing how the scoped model can be accessed and processed from the action.

In document Struts2 Black Book (Page 167-171)