org.chwf.servlet
Class InvocationContext

java.lang.Object
  |
  +--org.chwf.servlet.InvocationContext

public class InvocationContext
extends java.lang.Object

Invocation context that allows the controller to specify view urls and perform error handling. A new context is created for each invocation of a controller. As a rule, developers should not directly manipulate the InvocationContext; its methods are public only to facilitate out-of-container testing for view redirection.

Author:
Paul Strack

Constructor Summary
InvocationContext()
           
 
Method Summary
 void addViewParameter(java.lang.String parameter, java.lang.Object value)
          Sets a parameter for the view redirection.
 java.lang.Throwable getError()
          The exception thrown by the invoked method or null if the method was successful.
 java.lang.String getErrorPage()
          The error page, a URL without the context path.
 java.lang.String getRedirectURL()
          The redirection URL with parameter values appended.
 java.lang.Object getResult()
          The invoked method's result.
 java.lang.String getView()
          The view, a URL without the context path.
 void handleError(java.lang.String errorPage, java.lang.Throwable error)
          Specifies that the system redirect to the error page and pass it the specified error.
 void handleError(java.lang.Throwable error)
          Specifies that the system redirect to the default error page and pass it the specified error.
 boolean hasError()
          True if the invoked method threw an exception.
 void setDefaultErrorPage(java.lang.String url)
          Specify the default error page.
 void setDefaultView(java.lang.String url)
          Specify the default view.
 void setResult(java.lang.Object result)
          Set the invoked method's result.
 void setView(java.lang.String url)
          Set the view for the controller.
 void setView(java.lang.String url, java.lang.String parameter, java.lang.Object value)
          Set the view and a single parameter value for the controller.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InvocationContext

public InvocationContext()
Method Detail

setView

public void setView(java.lang.String url)
Set the view for the controller. When the invocation is complete, the system will redirect to this view. The view URL should be a "context-relative" URL. That is, if the URL begins with a "/", it should not include the application's context path. The URL will be resolved relative to the web application root directory. Controllers may call this method if they must manually control view redirection.

Parameters:
url - The URL to redirect to after processing is complete.

setView

public void setView(java.lang.String url,
                    java.lang.String parameter,
                    java.lang.Object value)
Set the view and a single parameter value for the controller. This simplifies the common situation when the controller needs to redirect to a JSP that requires a single parameter:

setView("/showItem.jsp", "itemId", item.getItemId());
This results in the redirection URL:

/{context-path}/showProject.jsp?itemId={itemId}
Parameters:
url - The URL to redirect to after processing is complete.
parameter - The parameter name.
value - The parameter value. This will be converted to a string.

addViewParameter

public void addViewParameter(java.lang.String parameter,
                             java.lang.Object value)
Sets a parameter for the view redirection. This parameter will be appended to the redirection URL. The parameter value will be converted to a String, and both the name and value will be URL encoded before redirection. This method may be called multiple times to set multiple values:

 addViewParameter("id", productId);
 addViewParameter("showSpecials", "true");
Parameters:
parameter - The parameter name.
value - The parameter value. This will be converted to a string.

handleError

public void handleError(java.lang.Throwable error)
Specifies that the system redirect to the default error page and pass it the specified error. Any specified view will be ignored.

Parameters:
error - The error.

handleError

public void handleError(java.lang.String errorPage,
                        java.lang.Throwable error)
Specifies that the system redirect to the error page and pass it the specified error. Any specified view will be ignored. This method lets the controller programmatically control error flow, allowing the controller to use different error pages for different kinds of errors.

Parameters:
errorPage - The error page.
error - The error.

getResult

public java.lang.Object getResult()
The invoked method's result.

Returns:
The invoked method's result.

setResult

public void setResult(java.lang.Object result)
Set the invoked method's result.

Parameters:
result - The invoked method's result.

getView

public java.lang.String getView()
The view, a URL without the context path.

Returns:
The view URL.

getErrorPage

public java.lang.String getErrorPage()
The error page, a URL without the context path.

Returns:
The error page.

getError

public java.lang.Throwable getError()
The exception thrown by the invoked method or null if the method was successful. It is forwarded to the error page.

Returns:
The error, or null if there is none.

hasError

public boolean hasError()
True if the invoked method threw an exception.

Returns:
True if the invoked method threw an exception.

getRedirectURL

public java.lang.String getRedirectURL()
The redirection URL with parameter values appended.

Returns:
The redirection URL with parameter values appended.

setDefaultView

public void setDefaultView(java.lang.String url)
Specify the default view.

Parameters:
url - The default view.

setDefaultErrorPage

public void setDefaultErrorPage(java.lang.String url)
Specify the default error page.

Parameters:
url - The default error page.


Copyright © 2002-2004, Paul Strack. All Rights Reserved.