⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baseaction.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    } // end tokenize()


    /**
     * Create an object of the specified class,
     * throwing a runtime exception if any error occurs.
     * If method succeeds, then helpers are guaranteed
     * to exist.
     *
     * @param request The HTTP request we are processing
     * @param helperClass The name of the class
     * @throws IllegalArgumentException if helper cannot be
     * instantiated
     */
    public Object createHelperObject(
            HttpServletRequest request,
            String helperClass) {

        StringBuffer sb = new StringBuffer();

        if (isDebug()) {
            sb.append(Log.HELPER_CREATING);
            sb.append(helperClass);
            servlet.log(sb.toString());
        }

            // Try the create
        Object helper = null;
        try {
            helper = Class.forName(helperClass).newInstance();
        }
        catch (Throwable t) {
           helper = null;
               // assemble message: {class}: {exception}
           sb.setLength(0);
           sb.append(Log.CREATE_OBJECT_ERROR);
           sb.append(Log.CLASS);
           sb.append(helperClass);
           sb.append(Log.SPACE);
           sb.append(Log.ACTION_EXCEPTION);
           sb.append(t.toString());
                // throw runtime exception
           throw new IllegalArgumentException(sb.toString());
        }

        if (isDebug()) {
            sb.setLength(0);
            sb.append(Log.HELPER_CREATED);
            sb.append(Log.CLASS);
            sb.append(helperClass);
            servlet.log(sb.toString());
        }

        return helper;

     } // createHelperObject()


    /**
     * Return an instance of the form-bean associated with the
     * specified name, if any; otherwise return <code>null</code>.
     * May be used to create an bean registered under a known name or
     * a form-bean name passed with the mapping.
     * <p>
     * It is not required that the form-bean specified here be an
     * ActionForm subclass. This allows other helper classes to be
     * registered as form-beans and then instantiated by the Action.
     *
     * @param request The HTTP request we are processing
     * @param helperName name of the form-bean helper
     */
    protected Object createHelperBean(
            HttpServletRequest request,
            String helperName) {

        StringBuffer sb = new StringBuffer();

        if (isDebug()) {
            sb.append(Log.HELPER_CREATING);
            sb.append(Log.NAME);
            sb.append(helperName);
            servlet.log(sb.toString());
        }

        Object bean = null;

        ActionFormBean formBean = servlet.findFormBean(helperName);
        if (formBean != null) {
            String className = null;
            className = formBean.getType();
            try {
                Class clazz = Class.forName(className);
                bean = clazz.newInstance();
            } catch (Throwable t) {
                bean = null;
                    // assemble message: {class}: {exception}
                sb.setLength(0);
                sb.append(Log.CREATE_OBJECT_ERROR);
                sb.append(Log.NAME);
                sb.append(helperName);
                sb.append(Log.SPACE);
                sb.append(Log.ACTION_EXCEPTION);
                sb.append(t.toString());
                String message = sb.toString();
                servlet.log(message);
                   // echo log message as error
                ActionErrors errors = getErrors(request,true);
                errors.add(ActionErrors.GLOBAL_ERROR,
                    new ActionError(Tokens.HELPER_ACCESS_ERROR,
                        message));
            }
            if (isDebug()) {
                sb.setLength(0);
                sb.append(Log.HELPER_CREATED);
                sb.append(Log.NAME);
                sb.append(helperName);
                servlet.log(sb.toString());
            }
        }
        else {
                // Not found
            sb.setLength(0);
            sb.append(Log.CREATE_OBJECT_ERROR);
            sb.append(Log.NAME);
            sb.append(helperName);
            sb.append(Log.SPACE);
            sb.append(Log.NOT_FOUND);
            String message = sb.toString();
            servlet.log(message);
                // echo log message as error
            ActionErrors errors = getErrors(request,true);
            errors.add(ActionErrors.GLOBAL_ERROR,
                new ActionError(Tokens.HELPER_ACCESS_ERROR,
                    message));
        }

        return bean;

    } // createHelperBean()



// --------------------------------------------------- EXTENSION POINTS


    /**
     * Optional extension point for pre-processing.
     * Default method does nothing.
     * To branch to another URI, return an non-null ActionForward.
     * If errors are logged (getErrors() et al),
     * default behaviour will branch to findFailure().
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The resonse we are creating
     */
    protected void preProcess(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {

        // override to provide functionality

    } // end preProcess()


     /**
      * Return the appropriate ActionForward for an
      * error condition.
      * The default method returns a forward to input,
      * when there is one, or "error" when not.
      * The application must provide an "error" forward.
      * An advanced implementation could check the errors
      * and provide different forwardings for different circumstances.
      * One possible error may be whether the form is null.
      *
      * @param mapping The ActionMapping used to select this instance
      * @param form The optional ActionForm bean for this request
      * @param request The HTTP request we are processing
      * @param response The resonse we are creating
      * @return The ActionForward representing FAILURE
      * or null if a FAILURE forward has not been specified.
      */
     protected ActionForward findFailure(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {

            // If input page, use that
        if (mapping.getInput()!=null)
            return (new ActionForward(mapping.getInput()));

            // If no input page, use error forwarding
        return mapping.findForward(Tokens.FAILURE);

    } // end findFailure()


    /**
     * Execute the business logic for this Action.
     * <p>
     * The default method logs the executeLogic() "event"
     * when the logging level is set to DEBUG.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The resonse we are creating
     */
    public void executeLogic(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
        throws Exception {

            // override to provide functionality, like
            // myBusinessObject.execute(form);
        servlet.log(Log.BASE_LOGIC_EXECUTING,Log.DEBUG);

    } // end executeLogic()


    /**
     * Token to print before short description
     * of an exception.
     */
    private static final String ERROR = " ERROR: ";

    /**
     * Token to print between short and long description
     * of an exception.
     */
    private static final String DETAILS = " DETAILS: ";


    /**
     * Process the exception handling for this Action.
     *
     * If Exception is subclass of BaseException, will
     * report on everything in chain.
     *
     * Default behaviour should suffice for most circumstances.
     * If overridden, if an alert is logged to the errors
     * queue (getErrors()), then default behaviour will branch
     * to findFailure().
     * :TODO: Use a StringBufferOUTPUTStream to capture trace for error queue
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The response we are creating
     */
    protected void catchException(
                ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response) {


            // Retrieve, log and print to error console
        Exception exception = getException(request);
        servlet.log(Log.ACTION_EXCEPTION, exception);
        exception.printStackTrace();

            // General error message
        ActionErrors errors = getErrors(request,true);
        errors.add(ActionErrors.GLOBAL_ERROR,
            new ActionError(Tokens.ERROR_GENERAL));

            // Wrap exception messages in ActionError
            // If chained, descend down chain
            // appending messages to StringBuffer
        StringBuffer sb = new StringBuffer();
        if (exception instanceof BaseException) {
           BaseException e = (BaseException) exception;
           e.getMessage(sb);
        }
        else {
            sb.append(ConvertUtils.LINE_FEED);
            sb.append(ERROR);
            sb.append(exception.toString());
            String message = exception.getMessage();
            if (null!=message) {
                sb.append(ConvertUtils.LINE_FEED);
                sb.append(DETAILS);
                sb.append(message);
                sb.append(ConvertUtils.LINE_FEED);
            }
            // :TODO: Use a StringBufferOUTPUTStream to capture trace
        }

        errors.add(ActionErrors.GLOBAL_ERROR,
            new ActionError(Tokens.ERROR_DETAIL,sb.toString()));

    } // end catchException()


    /**
     * Optional extension point for post-processing.
     * Default method does nothing.
     * This is called from a finally{} clause,
     * and so is guaranteed to be called after executeLogic() or
     * catchException().
     * Use getException() to check if error occured.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The resonse we are creating
     */
    protected void postProcess(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {

        // override to provide functionality

    } // end postProcess()


    /**
     * Return the appropriate ActionForward for the nominal,
     * non-error state.
     * The default returns mapping.findForward("continue");
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The response we are creating
     * @return The ActionForward representing SUCCESS
     * or null if a SUCCESS forward has not been specified.
     */
    protected ActionForward findSuccess(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {

        return mapping.findForward(Tokens.SUCCESS);

    } // end findSuccess()


// --------------------------------------------------- ANCESTOR METHODS


    /**
     * Wrapper around original <code>perform()</code> method
     * to call now-preferred <code>execute()</code> method,
     * This provides forward compatibility for Struts 1.0.x a
     * applications.
     * A 1.0.x application will call this method, which will return the
     * outcome of the <code>execute()</code> method.
     * A 1.1+ application will ignore this wrapper method, and call
     * <code>execute()</code> directly.
     * Good practice says that the <code>execute()</code> method
     * should cope with any exceptions and forward to the appropriate
     * error page.
     * But if any were to slip through, they are wrapped as
     * <code>ServetExceptions</code> and rethrown.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    public ActionForward perform(
                 ActionMapping mapping,
                 ActionForm form,
                 HttpServletRequest request,
                 HttpServletResponse response)
        throws IOException, ServletException {

        try {

            return execute(
                mapping,
                form,
                request,
                response
            );
        }

        catch (Exception e) {
            throw new ServletException(e);
        }
    }


    /**
     * Skeleton method that calls the other extension points
     * (or "hotspots") provided by this class. These are:
     * <ul>
     * <li><code>preProcess(),</code></li>
     * <li><code>executeLogic(),</code></li>
     * <li><code>catchException(),</code></li>
     * <li><code>postProcess(),</code></li>
     * <li><code>findFailure(),</code></li>
     * <li><code>findSuccess()</code></li>
     * </ul>
     * Typically, you can just override the other extension points
     * as needed, and leave this one as is.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    public ActionForward execute(
                 ActionMapping mapping,
                 ActionForm form,
                 HttpServletRequest request,
                 HttpServletResponse response) throws Exception {


            // Check for precondition errors; fail if found
        preProcess(mapping,form,request,response);

        if (isErrors(request)) {
            return findFailure(mapping,form,request,response);
        }

            // Try the logic; Call catchException() if needed
        try {

            executeLogic(mapping,form,request,response);
        }

        catch (Exception e) {
                // Store Exception; call extension point
            setException(request,e);
            catchException(mapping,form,request,response);
        }

        finally {
            postProcess(mapping,form,request,response);
        }

            // If errors queued, fail
        if (isErrors(request)) {
            return findFailure(mapping,form,request,response);
        }

            // Otherwise, check for messages and succeed (only 1_0)
        if ((isStruts_1_0()) && (isMessages(request))) {
            saveErrors(request,getMessages(request,false));
        }

        return findSuccess(mapping,form,request,response);

    } // end execute()

} // end BaseAction

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -