The RequestProcessor Class

 < Day Day Up > 



Struts uses the RequestProcessor class to perform the processing for all requests received by the ActionServlet. The RequestProcessor class takes each request and breaks its processing down into several small tasks. Each task is carried out by a separate method. This approach lets you customize the way each individual part of the request is processed. Each of these methods is aptly named with a prefix of process; for example, processMultipart( ) and processPath( ).

Table 5-1 lists and describes briefly each of the process*( ) methods from the RequestProcessor class (in the order they are executed).

Table 5-1: The process*( ) Methods of the RequestProcessor Class

Method

Description

processMultipart( )

Wraps multipart requests with a special wrapper class.

processPath( )

Determines the path that will be used to select an action to which processing is delegated.

processLocale( )

Saves the user's locale in session scope.

processContent( )

Sets the default content type for the response.

processNoCache( )

Sets no-cache HTTP headers for the response if necessary.

processPreprocess( )

Provides a hook for subclasses to override. It is used to tell the request processor whether or not to continue processing the request after this method has been called.

processCachedMessages( )

Removes cached ActionMessage objects from the session so that they are available only for one request.

processMapping( )

Selects the action mapping to use for the request.

processRoles( )

Checks if the current user has a role that is allowed to access the requested resource.

processActionForm( )

Creates a new Form Bean or retrieves one from the session for the request.

processPopulate( )

Populates the Form Bean returned from processActionForm( ) with data from the incoming request.

processValidate( )

Invokes the validate( ) method on the Form Bean returned from processActionForm( ) if necessary.

processForward( )

Processes the forward for the action mapping matching the current request path, if the matching mapping is specified to be a forward.

processInclude( )

Processes the include for the action mapping matching the current request path, if the matching mapping is specified to be an include.

processActionCreate( )

Creates or recycles an existing action to process the current request.

processActionPerform( )

Invokes the execute( ) method on the action returned from processActionCreate( ).

processForwardConfig( )

Forwards to the forward returned from processActionPerform( ).

By having each phase of the request processing cycle take place in a separate method, request processing can easily be customized. Simply create a custom request processor that extends the base RequestProcessor class and override the methods that need to be customized. For example, a custom request processor can apply a logged-in security check before any action is executed. The RequestProcessor class provides the processPreprocess( ) method hook expressly for this. The processPreprocess( ) method is called before actions are executed. The following example shows how to do this:

package com.jamesholmes.example; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.RequestProcessor; public class LoggedInRequestProcessor extends RequestProcessor {   protected boolean processPreprocess(     HttpServletRequest request,     HttpServletResponse response)   {     // Check if user is logged in.     // If so return true to continue processing,     // otherwise return false to not continue processing.     return (true);   } }

To use a custom request processor, you have to configure Struts to use it in the Struts configuration file:

<controller processor/>

Note 

When using Struts’ module feature, each module has its own request processor. Thus, if you want to apply a custom request processor to all modules, you must configure it in each module’s Struts configuration file.



 < Day Day Up > 



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2003
Pages: 134
Authors: James Holmes

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net