All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts.chain.commands.generic.CopyFormToContext Maven / Gradle / Ivy

/*
 * $Id: CopyFormToContext.java 471754 2006-11-06 14:55:09Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.chain.commands.generic;

import org.apache.struts.action.ActionForm;
import org.apache.struts.chain.commands.ActionCommandBase;
import org.apache.struts.chain.contexts.ActionContext;
import org.apache.struts.chain.contexts.ActionContextBase;
import org.apache.struts.config.ActionConfig;

/**
 * 

Subclass this command and configure it as part of a per-forward chain to * perform any necessary pre-population or other preparation for a form before * control is dispatched to the view layer.

* * @version $Id: CopyFormToContext.java 471754 2006-11-06 14:55:09Z husted $ */ public class CopyFormToContext extends ActionCommandBase { // ------------------------------------------------------ Instance Variables /** *

The name of a form bean as configured in a struts-config.xml file * for this module.

* *

Either actionPath or both this and scope are required configuration * properties.

*/ private String formName = null; /** *

The name of a scope, such as "request" or "session" in which the * form to be prepared will be placed for reference by the view and other * parts of Struts.

* *

Either actionPath or both this and * formName are required configuration properties.

*/ private String scope = null; /** *

The path of an <action> mapping as configured in * a struts-config.xml file for this module. This action * will be looked up, and its name and scope * values will be used as if those values were configured directly in this * instance's formName and scope * properties.

* *

Either this or both scope and * formName are required configuration properties.

*/ private String actionPath = null; /** * The context key under which the form which was looked up will be * stored. Defaults to "actionForm" but may be overridden in cases where * the "request" ActionForm must be preserved. */ private String toKey = ActionContextBase.ACTION_FORM_KEY; // ------------------------------------------------------ Properties /** *

Return ActionPath property.

* * @return ActionPath property */ public String getActionPath() { return this.actionPath; } /** *

Set ActionPath property.

* * @param actionPath New valuefor ActionPath */ public void setActionPath(String actionPath) { this.actionPath = actionPath; } /** *

Return FormName property.

* * @return FormName property */ public String getFormName() { return this.formName; } /** *

Set FormName property.

* * @param formName New valuefor FormName */ public void setFormName(String formName) { this.formName = formName; } /** *

Return Scope property.

* * @return Scope property */ public String getScope() { return this.scope; } /** *

Set Scope property.

* * @param scope New valuefor Scope */ public void setScope(String scope) { this.scope = scope; } /** *

Return ToKey property.

* * @return ToKey property */ public String getToKey() { return this.toKey; } /** *

Set ToKey property.

* * @param toKey New valuefor FormName */ public void setToKey(String toKey) { this.toKey = toKey; } // ------------------------------------------------------ /** *

Look up an ActionForm instance based on the configured properties of * this command and copy it into the Context. After this * command successfully executes, an ActionForm instance will exist in the * specified scope and will be available, for example for backing fields * in an HTML form. It will also be in the ActionContext * available for another command to do prepopulation of values or other * preparation.

* * @param actionContext Our ActionContext * @return TRUE if processing should halt * @throws Exception on any error */ public boolean execute(ActionContext actionContext) throws Exception { ActionForm form = findOrCreateForm(actionContext); if (isEmpty(getToKey())) { throw new IllegalStateException("Property 'toKey' must be defined."); } actionContext.put(getToKey(), form); return false; } /** *

Based on the properties of this command and the given * ActionContext, find or create an ActionForm instance for * preparation.

* * @param context ActionContextBase class that we are processing * @return ActionForm instance * @throws IllegalArgumentException On ActionConfig not found * @throws IllegalStateException On undefined scope and formbean * @throws IllegalAccessException On failed instantiation * @throws InstantiationException If ActionContext is not subsclass of * ActionContextBase */ protected ActionForm findOrCreateForm(ActionContext context) throws IllegalAccessException, InstantiationException { String effectiveFormName; String effectiveScope; if (!(isEmpty(this.getActionPath()))) { ActionConfig actionConfig = context.getModuleConfig().findActionConfig(this.getActionPath()); if (actionConfig == null) { throw new IllegalArgumentException( "No ActionConfig found for path " + this.getActionPath()); } effectiveFormName = actionConfig.getName(); effectiveScope = actionConfig.getScope(); } else { effectiveFormName = this.getFormName(); effectiveScope = this.getScope(); } if (isEmpty(effectiveScope) || isEmpty(effectiveFormName)) { throw new IllegalStateException("Both scope [" + effectiveScope + "] and formName [" + effectiveFormName + "] must be defined."); } return findOrCreateForm(context, effectiveFormName, effectiveScope); } /** *

Actually find or create an instance of ActionForm configured under * the form-bean-name effectiveFormName, looking in in the * ActionContext's scope as identified by * effectiveScope. If a form is created, it will also be * stored in that scope.

* *

NOTE: This specific method depends on the instance of * ActionContext which is passed being a subclass of * ActionContextBase, which implements the utility method * findOrCreateActionForm.

* * @param ctx The ActionContext we are processing * @param effectiveFormName the target form name * @param effectiveScope The target scope * @return ActionForm instnace, storing in scope if created * @throws InstantiationException If ActionContext is not subsclass of * ActionContextBase * @throws InstantiationException If object cannot be created * @throws IllegalArgumentException On form not found in/ scope * @throws IllegalAccessException On failed instantiation * @throws IllegalStateException If ActionContext is not a subclass of * ActionBase */ protected ActionForm findOrCreateForm(ActionContext ctx, String effectiveFormName, String effectiveScope) throws IllegalAccessException, InstantiationException { ActionContextBase context; try { context = (ActionContextBase) ctx; } catch (ClassCastException e) { throw new IllegalStateException("ActionContext [" + ctx + "]" + " must be subclass of ActionContextBase"); } ActionForm form = context.findOrCreateActionForm(effectiveFormName, effectiveScope); if (form == null) { throw new IllegalArgumentException("No form found under scope [" + effectiveScope + "] and formName [" + effectiveFormName + "]"); } return form; } /** *

Convenience method to test for an empty string.

* * @param test String to test * @return TRUE if test is null or zero-length */ private boolean isEmpty(String test) { return (test == null) || (test.trim().length() == 0); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy