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

📄 baseaction.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /home/cvs/jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/BaseAction.java,v 1.11 2004/03/14 14:32:19 husted Exp $
 * $Revision: 1.11 $
 * $Date: 2004/03/14 14:32:19 $
 *
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.struts.scaffold;


import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.*;

import org.apache.struts.util.MessageResources;

import org.apache.commons.scaffold.lang.BaseException;
import org.apache.commons.scaffold.lang.Log;
import org.apache.commons.scaffold.lang.Tokens;
import org.apache.commons.scaffold.text.ConvertUtils;


/**
 * Enhanced base Action.
 * See the documentation for the <code>execute</code> method for
 * operational details.
 * A <code>perform</code> method is also provided for backwards
 * compatibility with 1_0.
 * :TODO: Remove deprecations after formal 1.0 Scaffod release.
 *
 * @version $Revision: 1.11 $ $Date: 2004/03/14 14:32:19 $
 */
public class BaseAction extends Action {


// ---------------------------------------------------------- UTIITIES


    /**
     * Return whether Struts 1.0 compatibility should be used.
     *
     * @return True if built for Struts 1.0.x, false otherwise.
     * @deprecated Will be removed after Struts 1.1 final ships.
     */
    private final boolean isStruts_1_0() {
        return true;  // Struts 1.0.x
//      return false; // Struts 1.1
    }

// ---------------------------------------------------------------- Log

    /**
     * Is logging set this level or higher?
     *
     * @param level The debug level to test
     */
    protected boolean isLogLevel(int level) {
        return (servlet.getDebug()>=level);
    }


    /**
     * Is logging set to debugging?
     */
    protected boolean isDebug() {
        return (servlet.getDebug()>=Log.DEBUG);
    }


    /**
     * Is logging set to verbose?
     */
    protected boolean isVerbose() {
        return (servlet.getDebug()>=Log.VERBOSE);
    }


// ---------------------------------------------------------- Exception


    /**
     * Set exception to request under public key [Action.EXCEPTION].
     *
     * @param request The HTTP request we are processing
     * @param e The new Exception
     */
    protected void setException(
            HttpServletRequest request,
            Exception e) {
        request.setAttribute(EXCEPTION_KEY,e);
    }


    /**
     * Return exception stored in request under public key [Action.EXCEPTION].
     *
     * @param request The HTTP request we are processing
     */
    protected Exception getException(HttpServletRequest request) {
        return (Exception) request.getAttribute(EXCEPTION_KEY);
    }


// ------------------------------------------------------------- Locale

    /**
     * Return the framework locale object for the user session
     * for given request.
     * If no session is set, or if the session has no locale
     * set, the default locale is returned.
     *
     * @param request The HTTP request we are processing
     * author  Fran鏾is Rey (FREY - francois.rey@capco.com)
     * author  Eric Bariaux (EBRX - eric.bariaux@capco.com)
     */
    protected Locale getLocale(HttpServletRequest request) {

        Locale result = null;
        HttpSession session = request.getSession();
        if (session!=null) {
            result = (Locale) session.getAttribute(Action.LOCALE_KEY);
            if (result == null) result = Locale.getDefault();
        } else {
            result = Locale.getDefault();
        }

        return result;

    } // end getLocale()


    /**
     * Set the framework locale object in the session for this request.
     * If a session context does not exist, one is created.
     *
     * @param request The HTTP request we are processing
     * @param locale The locale to use for this session
     */
    protected void setLocale(
            HttpServletRequest request,
            Locale locale) {

        HttpSession session = request.getSession(true);
        session.setAttribute(Action.LOCALE_KEY,locale);

    } // end setLocale()



// ------------------------------------------------------ Remote Node


    /**
     * Returns the RemoteHost IP as an Integer.
     */
    protected Integer getRemoteNode(HttpServletRequest request) {
        return new Integer(0); // :FIXME: Non functional
    }


// ------------------------------------------------------ Remote Server

    /**
     * Default name for a remote server that may be found
     * in application scope ["REMOTE_SERVER"].
     */
    public static String REMOTE_SERVER_KEY = "REMOTE_SERVER";


    /**
     * Returns name of result server to be used by this Action,
     * e.g, RESULT_SERVER_KEY.
     */
    protected String getRemoteServerName() {
        return REMOTE_SERVER_KEY;
    }


    /**
     * Checks application scope for the remote server object
     * specified by <code>getRemoteServerName</code>
     */
    protected Object getRemoteServer() {
return servlet.getServletContext().getAttribute(getRemoteServerName());
    }

// ----------------------------------------------------------- Messages

    /**
     * Return the application properties for this web application,
     * if any.
     */
    protected Properties getProperties() {
        return (Properties) servlet.getServletContext().getAttribute(
            org.apache.commons.scaffold.lang.Tokens.PROPERTIES_KEY);
    }


    /**
     * Return the application resources for this web application,
     * if any.
     */
    protected MessageResources getMessageResources() {
        return servlet.getResources();
    }


    /**
     * Number of replacement parameters permitted in Struts 1.0.
     * See also saveConfirm.
     */
    private static int CONFIRM_MAX = 5; // (Message Key, plus 1..4)


    /**
     * Retrieves a base messages and up to four replaceable
     * parameters from a List, and adds them to an ActionErrors
     * collection.
     *
     * // :FIXME: In 1.1 this should be updated to use the
     * new ActionMessages superclass.
     *
     * @param request The request we are servicing
     * @param alerts Our ActionErrors collection
     * @param list our list of replaceable parameters
     */
    protected void mergeAlerts(
            HttpServletRequest request,
            ActionErrors alerts,
            List list) {

        if ((null!=list) && (0!=list.size())) {

            int size = list.size();
                // Struts 1.0 allows up to 4 parameters, 1..4
            if (size > CONFIRM_MAX) size = CONFIRM_MAX;
            Object[] confirm = new Object[size];

            for (int i=0; i<size; i++) {
                confirm[i] = list.get(i);
            }

            switch (size) {
               case 5:
                    alerts.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError((String) confirm[0],
                            confirm[1],confirm[2],confirm[3],
                            confirm[4]));
                    break;
               case 4:
                    alerts.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError((String) confirm[0],
                            confirm[1],confirm[2],confirm[3]));
                    break;
               case 3:
                    alerts.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError((String) confirm[0],
                            confirm[1],confirm[2]));
                    break;
               case 2:
                    alerts.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError((String) confirm[0],
                            confirm[1]));
                    break;
               case 1:
                    alerts.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError((String) confirm[0]));
            }

        }

    } // end mergeAlerts()


    /**
     * Check for pending message collection.
     * If it doesn't exist, and create is true, a new collection is
     * returned.
     *
     * :FIXME: In 1.1 this should be the ActionMessage superclass
     * @param request The HTTP request we are processing
     * @param create Whether to create a new collection if one does
     * not exist
     * @return The pending ActionError queue
     */
    protected ActionErrors getMessages(
            HttpServletRequest request,
            boolean create) {

            // :FIXME: In 1.1 should use public static
        final String
            MESSAGE_KEY = "org.apache.struts.action.ACTION_MESSAGE";

        ActionErrors alerts = (ActionErrors)
            request.getAttribute(MESSAGE_KEY);

        if ((null==alerts) && (create)) {
            alerts = new ActionErrors();
                // Bypass Action.SaveMessage() since it
                // won't accept a blank collection
            request.setAttribute(MESSAGE_KEY,alerts);
        }

        return alerts;

    } // end getMessages()


    /**
     * Merge incoming messages and save to messages
     * collection. The collection is automatically saved to the request,
     * so <code>Action.saveErrors(request,errors)</code> does not need
     * to be called. If this method called more than once, the new
     * messages are appended.
     * <p>
     * This method is upwardly compatabile with Struts 1.1 and uses the
     * messages queue introduced with that release.
     *
     * @param request The HTTP request we are processing
     * @param list The ResourceBundle token followed by 0 or more
     * parameters
     */
    protected void saveMessages(
            HttpServletRequest request,
            List list) {

        ActionErrors alerts = getMessages(request,true);
        mergeAlerts(request,alerts,list);

    } // end saveMessages()


    /**
     * Return whether there is an informational alert collection pending.
     *
     * @param request The HTTP request we are processing
     * @return True if an informational alert collection exists
     */
    protected boolean isMessages(HttpServletRequest request) {
        return (null!=getMessages(request,false));
    }


    /**
     * Merge incoming messages and save to errors
     * collection. The collection is automatically saved to the request,
     * so <code>Action.saveErrors(request,errors)</code> does not need
     * to be called. If this method called more than once, the new
     * messages are appended.
     *
     * @param request The HTTP request we are processing
     * @param list The ResourceBundle token followed by 0 or more
     * parameters
     */
    protected void saveErrors(
            HttpServletRequest request,
            List list) {

        ActionErrors alerts = getErrors(request,true);
        mergeAlerts(request,alerts,list);

    } // end saveErrors


    /**
     * Return whether there is an errors alert collection pending.
     *
     * @param request The HTTP request we are processing
     * @return True if an errors alert collection exists
     */
    protected ActionErrors getErrors(
            HttpServletRequest request,
            boolean create) {

        ActionErrors alerts = (ActionErrors)
            request.getAttribute(ERROR_KEY);

        if ((null==alerts) && (create)) {

            alerts = new ActionErrors();
                // Bypass Action.SaveError() since it
                // won't accept a blank collection
            request.setAttribute(ERROR_KEY,alerts);

        }

        return alerts;

    } // end getErrors()


    /**
     * Return whether there is an errors alert collection pending.
     *
     * @param request The HTTP request we are processing
     * @return True if an errors alert collection exists
     */
    protected boolean isErrors(HttpServletRequest request) {
        return (null!=getErrors(request,false));
    }


// ------------------------------------------------------------ Helpers

    /**
     * Default separator character for list of tokens [";"] (semi-colon).
     */
    public static final String TOKEN_SEP = ";";


    /**
     * Return separator character for list of tokens
     */
    public String getTokenSep() {
        return TOKEN_SEP;
    }


    /**
     * Return array of tokens,
     * using the result of <code>getTokeSep()</code> as the
     * separator.
     * Blanks are trimmed from tokens.
     *
     * @param parameter The string to tokenize into an array
     */
    public String[] tokenize(String parameter) {
        
        return ConvertUtils.tokensToArray(parameter,getTokenSep());
             
/*        
        StringTokenizer tokenizer =
            new StringTokenizer(parameter,getTokenSep());
        int i = 0;
        String[] tokens = new String[tokenizer.countTokens()];
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken().trim();
            if ((token==null) || (token.length()==0)) continue;
            tokens[i++] = token;
        }
        return tokens;
*/

⌨️ 快捷键说明

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