Java tutorial
/* * 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.util.Iterator; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jp.terasoluna.fw.web.thin.BlockageControlFilter; import jp.terasoluna.fw.web.thin.ServerBlockageControlFilter; 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; /** * * tH??[h??U???s?B * * <p> * NGXgp??[^p?AJ??B<br> * StrutsDispatchAction?ALookupDispatchActiondl?A??B * </p> * <p> * ?ANGXgp??[^L?[?Bean`t@CKv?s * ?iftHgl"event"?j?B * </p> * <p> * LL`t@C * NGXgp??[^L?[custom-event??B * </p> * <p> * <strong>Bean`t@C?</strong> * <code><pre> * <bean name="/dispatch" scope="prototype" * class="jp.terasoluna.fw.web.struts.actions.DispatchAction"> * <strong><property name="event"> * <value>custom-event</value> * </property></strong> * </bean> * </pre></code> * </p> * <p> * ?AJ?BD???B * <ol> * <li>?LwNGXgp??[^L?[l?A * ?"forward_"?A * "forward_"?</li> * <li>NGXgp??[^L?[?"forward_"t?A * "forward_"?</li> * <li>"default" * ?ievent="XXXX"?A"forward_XXXX" * ?s?J?w???j</li> * </ol> * * ?L?AJ"#input" * ?Astruts-config.xmlinput?J??B<br> * "#input"?Astruts-config.xmlforwardvfeJ??B * </p> * <p> * <strong>struts-config.xml?</strong> * <code><pre> * <action path="/dispatch" * name="_sampleForm" * scope="session" * input="/prev.jsp"> * <forward name="regist" path="/userRegist.do"/> * <forward name="search" path="/userSearch.do"/> * <forward name="update" path="/userUpdate.do"/> * <forward name="decide" path="/prev.do"/> * <forward name="default" path="/prev.do"/> * </action> * </pre></code> * </p> * <p> * <strong>JSPL?q</strong> * <code><pre> * ?E?E?E * <html:radio property="<strong>custom-event</strong>" value="<strong>forward_regist</strong>"/> * <html:radio property="<strong>custom-event</strong>" value="<strong>forward_search</strong>"/> * <html:radio property="<strong>custom-event</strong>" value="<strong>forward_update</strong>"/> * <html:radio property="<strong>custom-event</strong>" value="<strong>forward_#input</strong>"/> * <html:submit property="<strong>forward_decide</strong>" value=""/> * <html:submit value=""/> * ?E?E?E * </pre></code> * NGXgp??[^l"forward_" + _?tH??[h`?B * </p> * <p> * e??s??J??B * <ol> * <li>1WI{^???A * J"regist"?AJ?"/userRegist.do"</li> * <li>4WI{^???A * J"#input"?AJ?"/prev.do"</li> * <li>WI{^I?A???A * J"decide"?AJ?"/prev.do"</li> * <li>???A * J"default"?AJ?"/prev.do"</li> * </ol> * </p> * */ public class DispatchAction extends ActionEx { /** * ?ONX?B */ private static Log log = LogFactory.getLog(DispatchAction.class); /** * NGXgp??[^?Uw???A * ftHgJ?_?tH??[h?B */ private static final String FORWARD_DEFAULT = "default"; /** * ?Uw?A * NGXgp??[^L?[vtBbNX?B */ private static final String FORWARD_PREFIX = "forward_"; /** * J?\v?peB?B */ private String event = null; /** * J?\v?peB??B * * @param value J?v?peB */ public void setEvent(String value) { this.event = value; } /** * tH??[h??U???s?B * <p> * tH??[h??U?AT?[o?tO * ???B<br> * ?J??AdoDetamineForward()?B * </p> * * @param mapping ANV}bsO * @param form ANVtH?[ * @param req <code>HTTP</code>NGXg * @param res <code>HTTP</code>X|X * @return J?? */ @Override public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { if (log.isDebugEnabled()) { log.debug("doExecute() called."); } // NGXgLZtO?mF?A // cancelled()?\bh?s?B if (isCancelled(req)) { ActionForward af = cancelled(mapping, form, req, res); if (af != null) { return af; } } if (event == null) { // eventw?A"event"ftHg??B event = "event"; } String forward = doDetermineForward(req.getParameterMap(), event); ActionForward actionForward = null; if ("#input".equalsIgnoreCase(forward)) { actionForward = new ActionForward(mapping.getInput()); } else { actionForward = mapping.findForward(forward); } // tH??[h????A"default" w // ANVtH??[hp?B if (actionForward == null) { if (log.isWarnEnabled()) { log.warn("forward name[" + forward + "] is invalid by user request."); } actionForward = mapping.findForward(FORWARD_DEFAULT); } // tH??[h??`FbNL // THRU_FILTERtO?? // T?[o? req.removeAttribute(ServerBlockageControlFilter.SERVER_BLOCKAGE_THRU_KEY); // ? req.removeAttribute(BlockageControlFilter.BLOCKAGE_THRU_KEY); if (log.isDebugEnabled()) { log.debug("forward = " + forward + " (" + ((actionForward == null) ? "null" : actionForward.getPath()) + ")"); } return actionForward; } /** * NGXgp??[^tH??[h?fBXpb`?B * paramsnull???AftHgp?B * * @param params NGXgp??[^?i}bv`?j * @param event ANV}bsOwCxg * @return ?U?_?tH??[h */ protected String doDetermineForward(Map params, @SuppressWarnings("hiding") String event) { if (params != null) { if (exists(params, event)) { String[] eventValues = (String[]) params.get(event); for (int i = 0; i < eventValues.length; i++) { if (eventValues[i].startsWith(FORWARD_PREFIX)) { return eventValues[i].substring(FORWARD_PREFIX.length()); } } } Iterator iter = params.keySet().iterator(); while (iter.hasNext()) { String key = (String) iter.next(); if (key.startsWith(FORWARD_PREFIX)) { String forward = key.substring(FORWARD_PREFIX.length()); if (forward.endsWith(".x") || forward.endsWith(".y")) { forward = forward.substring(0, forward.length() - 2); } return forward; } } } return FORWARD_DEFAULT; } /** * NGXgp??[^?Anamew?p??[^ * ??B * * @param params NGXgp??[^?i}bv`?j * @param name NGXgp??[^ * @return NGXgp??[^? <code>true</code> */ protected boolean exists(Map params, String name) { return params.containsKey(name); } /** * NGXgLZtO???J? * ?B?_?AActionForwardnullp?A * LZJ??A * NXp?NXI?[oCh?\bhKv?B * * @param mapping ANV}bsO * @param form ANVtH?[ * @param request HTTPNGXg * @param response HTTPX|X * @return ANVtH??[h */ protected ActionForward cancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return null; } }