jp.terasoluna.fw.web.struts.actions.ForwardAction.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.struts.actions.ForwardAction.java

Source

/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * 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 jp.terasoluna.fw.web.struts.actions;

import java.io.IOException;

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

import jp.terasoluna.fw.exception.SystemException;
import jp.terasoluna.fw.util.PropertyUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * P?tH??[hANV?B
 *
 * <p>
 *  ActionEx@\?iJ?O?o?EgUNVg?[N`FbN?jp??A
 *  JSPtH??[hANV?B
 *  StrutsForwardActionl
 *  struts-config.xml&lt;action&gt;vf
 *  parameter?w?tH??[h?B
 *  parameter?????A
 *  _?tH??[h?usuccess?vANVtH??[h?B
 *  tH??[h?????A
 *  SC_NOT_FOUND?i404?jG?[?B
 *  *.jspt@C?ANZX~???A
 *  JSP??oP?\
 *  ANVpstruts-config.xml
 *  Gg??Kv?B
 *  ActionEx#execute()?s???A
 *  p??B
 * </p>
 * 
 *<br>
 *
 * <p>Bean`t@Cystruts-config.xmlL?q?B</p>
 *
 * :<br>
 * <p>
 *  <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *   <legend><strong>Bean`t@C?</strong></legend>
 *   <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *  <code><pre>
 *  &lt;bean name="/foo" scope="prototype"
 *      <strong>class="jp.terasoluna.fw.web.struts.actions.ForwardAction"</strong>&gt
 *  &lt;/bean&gt
 *  </pre></code>
 *   </fieldset>
 *  </fieldset>
 * </p>
 * 
 * <p>
 *  <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *   <legend><strong>struts-config.xml?</strong></legend>
 *   <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *    <code><pre>
 *  &lt;action path="/foo"
 *          parameter="/foo.jsp"&gt;
 *  &lt;/action&gt;
 *    </pre></code>
 *   </fieldset>
 *   
 *   <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *    <code><pre>
 *  &lt;action path="/foo"
 *          parameter="/foo.jsp"&gt;
 *    &lt;forward name="success" path="/foo.jsp" module="/sub1" redirect="true"&gt;
 *  &lt;/action&gt;
 *    </pre></code>
 *   </fieldset>
 *  </fieldset>
 * </p>
 *
 *<br>
 *
 * <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *  <legend><strong>contextRelativetB?[h</strong></legend>
 * <strong>parameter?</strong>pXw@?X?B
 * 
 * <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *  <legend><strong>contextRelative?lpXw@</strong></legend>
 *  <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *   <legend><strong>true</strong>??</legend>
 *   parameter??AReLXg?[g?pXw?B<br>
 *   {IW?[Jgp?B
 *   <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *    <code><pre>
 *  &lt;action path="/pagelinkForward"
 *          parameter="<strong>/pagelink/</strong>sc2401.jsp"/&gt;
 *    </pre></code>
 *   </fieldset> 
 *  </fieldset> 
 *  <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *   <legend><strong>false</strong>??</legend>
 *   parameter??AW?[pXw?s?B<br>
 *  W?[J_CNg?s???A
 *  &lt;forward&gt;vfgp?B
 *   <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *    <code><pre>
 *  &lt;action path="/pagelinkForward"
 *          parameter="<strong>/</strong>sc2401.jsp"/&gt;
 *    </pre></code>
 *   </fieldset> 
 *  </fieldset> 
 * </fieldset> 
 * 
 * 
 * <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *  <legend><strong>contextRelative?@</strong></legend>
 *  system.properties?L?q?B<br>
 *  ???? <strong>false</strong> ?B<br>
 *  <fieldset style="border:1pt solid black;padding:10px;width:100%;">
 *   <legend><strong>system.properties</strong></legend>
 *    <code><pre>
 * forwardAction.contextRelative=true
 *    </pre></code>
 *  </fieldset> 
 * </fieldset> 
 * </fieldset> 
 *
 *<br>
 *
 */
public class ForwardAction extends ActionEx {

    /**
     * ?ONX?B
     */
    private static Log log = LogFactory.getLog(ForwardAction.class);

    /**
     * G?[y?[W?i404?jJsG?[R?[h?B
     */
    private static final String FORWARD_ERRORPAGE_ERROR = "error.forward.errorpage";

    /**
     * _?tH??[h?B
     */
    private static final String FORWARD_SUCCESS = "success";

    /**
     * contextRelative?lv?peBL?[
     */
    private static final String FORWARD_ACTION_CONTEXT_RELATIVE_KEY = "forwardAction.contextRelative";

    /**
     * parameter??J?
     * ANVtH??[hZbg?B
     * parameter?????A
     * J?_?tH??[h"success"
     * ANVtH??[h?B
     * ????A?i404?jG?[?B
     *
     * @param mapping ANV}bsO
     * @param form ANVtH?[
     * @param req HTTPNGXg
     * @param res HTTPX|X
     * @return J?ANVtH??[h
     */
    @SuppressWarnings("deprecation")
    @Override
    public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest req,
            HttpServletResponse res) {
        if (log.isDebugEnabled()) {
            log.debug("doExecute() called.");
        }

        // parameter??itH??[h??j
        String path = mapping.getParameter();

        // ANVtH??[h??
        ActionForward retVal = null;

        if (path == null) {

            // ActionMappingActionForward
            retVal = mapping.findForward(FORWARD_SUCCESS);

            // ActionFoward???
            if (retVal == null) {
                // parameter??Aforwardvf????A
                // ?i404?jG?[p
                try {
                    res.sendError(HttpServletResponse.SC_NOT_FOUND);
                } catch (IOException e) {
                    log.error("Error page(404) forwarding failed.");

                    throw new SystemException(e, FORWARD_ERRORPAGE_ERROR);
                }
                return null;
            }
            return retVal;
        }

        retVal = new ActionForward(path);

        // contextRelative?l
        String contextRelativeStr = PropertyUtil.getProperty(FORWARD_ACTION_CONTEXT_RELATIVE_KEY,
                Boolean.FALSE.toString());
        Boolean contextRelative = new Boolean(contextRelativeStr);

        // ActionForwardcontextRelativel?
        retVal.setContextRelative(contextRelative);
        if (log.isDebugEnabled()) {
            StringBuilder debugLog = new StringBuilder();
            debugLog.append("contextRelative:");
            debugLog.append(contextRelative);
            log.debug(debugLog.toString());
        }

        return retVal;
    }

}