Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Struts RequestProcessor override...

1 view
Skip to first unread message

schmen

unread,
Feb 13, 2004, 10:19:33 PM2/13/04
to
I have subclassed the Struts RequestProcessor (Controller) in an
effort to centralize some of my business logic. On occassion I will
need to override the original service request with a new forward
action. Any idea what method call(s) I need to make?

Thanks for any help,
schmen

Sudsy

unread,
Feb 14, 2004, 12:08:32 AM2/14/04
to

Take a look at the javadocs for RequestProcessor and note those methods
which return a boolean. The return value indicates whether or not to
continue processing the request. In my processPreprocess method I can
examine the session context and decide whether or not the user has
logged-in to the application. If so then I simply return true. Else I
can invoke doForward and return false.
Sorry I don't have a URL I can point you to...

schmen

unread,
Feb 14, 2004, 11:58:00 AM2/14/04
to
Sudsy <bitbu...@hotmail.com> wrote in message news:<402DAD50...@hotmail.com>...

Thanks for your help, I am still having problems and I think that it
would be helpful if you were to provide the code snipit that calls the
.doForward() method within the RequestProcessor class, as well as your
<forward> entry definition within your struts-config file.

Thanks again.

Sudsy

unread,
Feb 14, 2004, 2:25:38 PM2/14/04
to
schmen wrote:
> Thanks for your help, I am still having problems and I think that it
> would be helpful if you were to provide the code snipit that calls the
> .doForward() method within the RequestProcessor class, as well as your
> <forward> entry definition within your struts-config file.

I have two objections: I don't like to waste other people's bandwidth,
and I usually charge money for delivering this kind of code. That said,
I'll make an exception so that this can end up in the archives and
might assist others in the future.
I use a global forward for my login, the page I redirect users to when
they don't have credentials in session scope. Here's some snippets from
struts-config.xml:


<global-forwards>
<forward name="login" path="/login"/>
</global-forwards>
<action-mappings>
<action name="loginForm"
path="/login"
type="com.onlinetrs.trs.LoginAction"
scope="request"
validate="true"
input="/WEB-INF/jsp/login.jsp">
<forward name="continue" path="/index.jsp"/>
</action>
</action-mappings>
<controller
contentType="text/html;charset=UTF-8"
debug="0"
nocache="true"
processorClass="com.onlinetrs.trs.MyRequestProcessor"/>

Here's the code for MyRequestProcessor:

package com.onlinetrs.trs;

import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.logging.Log;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import java.io.IOException;

public class MyRequestProcessor extends RequestProcessor {

private static final String loginPath = "/login";

public boolean processPreprocess( HttpServletRequest req,
HttpServletResponse resp ) {
HttpSession sess = req.getSession( true );
Integer userID = (Integer) sess.getAttribute( "user" );
String requestPath = null;
String forwardPath = null;
ActionMapping loginActionMapping = null;
boolean rc = false;

/*
* if userID in session then already logged-in
*/

if( userID != null )
return( true );

/*
* get the request path
*/

try {
requestPath = processPath( req, resp );
}
catch( IOException e ) {
log.error( e.toString() );
return( true );
}

/*
* if it's login then continue
*/

if( requestPath.equals( loginPath ) )
return( true );

/*
* save the original request
*/

sess.setAttribute( "originalRequest", requestPath );

/*
* get the action for the login
*/

try {
loginActionMapping = processMapping( req, resp,
loginPath );
forwardPath =
loginActionMapping.getInputForward().getPath();
doForward( forwardPath, req, resp );
return( false );
}
catch( Exception e ) {
log.error( e.toString() );
return( true );
}
}
}

Fair enough?

schmen

unread,
Feb 14, 2004, 7:00:59 PM2/14/04
to
More than fair, and very generous of you.

Thanks a lot!

Sudsy <bitbu...@hotmail.com> wrote in message news:<402E763...@hotmail.com>...

Sudsy

unread,
Feb 14, 2004, 11:16:05 PM2/14/04
to
schmen wrote:
> More than fair, and very generous of you.

More points, before somebody else "jumps the gun":
- don't top post
- trim the reply

ps. Send money!

0 new messages