📄 requestprocessor.java
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.config.ExceptionConfig;import org.apache.struts.taglib.html.Constants;import org.apache.struts.tiles.TilesRequestProcessor;import net.sf.irunninglog.util.Utilities;/** * Extension of the <code>org.apache.struts.tiles.TilesRequestProcessor</code> * class used to handle any application-specific request processing logic. * * <p/> * * The main purpose of this class is to handle requests that represent a * cancellation. This request processor contains all of the logic needed * to handle form population, validation, etc. when a user performs a * cancellation. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.4 $ $Date: 2005/06/30 02:01:05 $ * @since iRunningLog 1.0 */public class RequestProcessor extends TilesRequestProcessor { /** <code>Log</code> instance for this class. */ private static final Log LOG = LogFactory.getLog(RequestProcessor.class); /** Name of the request attribute used to store exceptions. */ public static final String REQUEST_ATTR_EXCEPTION = "Exception"; /** Name of the forward to be used when an action is successful. */ public static final String FORWARD_SUCCESS = "Success"; /** Name of the forward to be used when an action is cancelled. */ public static final String FORWARD_CANCEL = "Cancel"; /** * Process a servlet request, and either create a response or dispatch to * another resource. This method is currently only a placeholder, used * for logging, etc. The real work of processing requests is left to * the Struts request processor. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @throws IOException If an input/output error occurs * @throws ServletException If a processing exception occurs */ public void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (LOG.isDebugEnabled()) { LOG.debug("process: Processing request " + Utilities.toString(request)); } if (LOG.isDebugEnabled()) { LOG.debug("process: Session contents " + Utilities.toString(request.getSession(false))); } super.process(request, response); } /** * Ask the specified <code>Action</code> instance to handle this request. * If the request represents a cancellation, and a 'Cancel' forward has * been defined, that forward will be returned. Otherwise, the request * will be processed normally. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param action The <code>Action</code> instance to be used * @param form The <code>ActionForm</code> instance to pass to this * <code>Action</code> * @param mapping The <code>ActionMapping</code> instance to pass to this * <code>Action</code> * @return The <code>ActionForward</code> returned by the * <code>Action</code>, or the mapping's 'Cancel' forward if the * request is a cancellation and a 'Cancel' forward has been * specified * @throws IOException If an input/output error occurs * @throws ServletException If a processing exception occurs */ protected ActionForward processActionPerform(HttpServletRequest request, HttpServletResponse response, Action action, ActionForm form, ActionMapping mapping) throws IOException, ServletException { ActionForward forward = mapping.findForward(FORWARD_CANCEL); // If the request is a cancellation and a 'Cancel' forward exists in // the mapping, return that mapping. Otherwise, process normally. if (isCancellation(request) && forward != null) { if (LOG.isDebugEnabled()) { LOG.debug("processActionPerform: Cancelling, returning" + " forward " + forward.getName() + " path=" + forward.getPath()); } return forward; } else { forward = super.processActionPerform(request, response, action, form, mapping); if (LOG.isDebugEnabled()) { LOG.debug("processActionPerform: Returning" + " forward " + forward.getName() + " path=" + forward.getPath()); } return forward; } } /** * Populate the properties of the specified <code>ActionForm</code> * instance from the request parameters included with this request. This * will handle user cancellations, and will not populate the form if the * request was initiated by a cancellation. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param form The <code>ActionForm</code> instance we are populating * @param mapping The <code>ActionMapping</code> we are using * @throws ServletException If a processing exception occurs */ protected void processPopulate(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping) throws ServletException { if (isCancellation(request)) { if (LOG.isDebugEnabled()) { LOG.debug("processPopulate: Cancelling, do not populate"); } // Do not populate the form if the request is a cancellation return; } else { super.processPopulate(request, response, form, mapping); } } /** * Call the validate method of the specified <code>ActionForm</code>, and * forward to the input path if there were any errors. If the request is a * cancellation, this will return true to indicate that processing should * continue. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param form The <code>ActionForm</code> instance we are populating * @param mapping The <code>ActionMapping</code> we are using * @return True if we should continue processing (or the request is a * cancellation), or false if we have already forwarded control * back to the input form * @throws IOException If an input/output error occurs * @throws ServletException If a processing exception occurs */ protected boolean processValidate(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping) throws IOException, ServletException { if (isCancellation(request)) { if (LOG.isDebugEnabled()) { LOG.debug("processValidate: Cancelling, return true"); } // Do not perform any validations if the request is a cancellation return true; } else { return super.processValidate(request, response, form, mapping); } } /** * Determine whether a given request is a cancellation. * * @param request The request being processed * @return True if the request is a cancellation, false otherwise */ private boolean isCancellation(HttpServletRequest request) { return (request.getParameter(Constants.CANCEL_PROPERTY) != null) || (request.getParameter(Constants.CANCEL_PROPERTY_X) != null); } /** * Handle any exception encountered during application execution. This * method will use the Struts framework to perform any declared exception * handling. If no exception handling has been defined for a given * exception, the application will log the error and forward control to an * exception handling page. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param exception The exception we are processing * @param form The <code>ActionForm</code> instance we are populating * @param mapping The <code>ActionMapping</code> we are using * @return The <code>ActionForward</code> to which control should be * forwarded * @throws IOException If an input/output error occurs * @throws ServletException If a processing exception occurs */ protected ActionForward processException(HttpServletRequest request, HttpServletResponse response, Exception exception, ActionForm form, ActionMapping mapping) throws IOException, ServletException { Class exceptionClass = exception.getClass(); if (LOG.isDebugEnabled()) { LOG.debug("processException: Handling an exception of class " + exceptionClass); } ExceptionConfig config = mapping.findException(exception.getClass()); if (config == null) { // No handling defined. Redirect to exception handling page. if (LOG.isDebugEnabled()) { LOG.debug("processException: No exception config found - using" + " default exception handling"); } request.setAttribute(REQUEST_ATTR_EXCEPTION, exception); if (LOG.isDebugEnabled()) { LOG.debug("processException: Putting the following exception" + " into the request " + exception); } return mapping.findForward(REQUEST_ATTR_EXCEPTION); } else { // Handling defined in struts-config. Use that. if (LOG.isDebugEnabled()) { LOG.debug("processException: Exception config found - using" + "declared exception handling"); } return super.processException(request, response, exception, form, mapping); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -