FileDocCategorySizeDatePackage
Form.javaAPI DocExample18789Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

Form

public class Form extends ClosingUIBean

Renders HTML an input form.

The remote form allows the form to be submitted without the page being refreshed. The results from the form can be inserted into any HTML element on the page.

NOTE:

The order / logic in determining the posting url of the generated HTML form is as follows:-

  1. If the action attribute is not specified, then the current request will be used to determine the posting url
  2. If the action is given, Struts will try to obtain an ActionConfig. This will be successfull if the action attribute is a valid action alias defined struts.xml.
  3. If the action is given and is not an action alias defined in struts.xml, Struts will used the action attribute as if it is the posting url, separting the namespace from it and using UrlHelper to generate the final url.

Examples


<s:form ... />

Fields Summary
public static final String
OPEN_TEMPLATE
public static final String
TEMPLATE
private int
sequence
protected String
onsubmit
protected String
action
protected String
target
protected String
enctype
protected String
method
protected String
namespace
protected String
validate
protected String
portletMode
protected String
windowState
protected String
acceptcharset
protected boolean
enableDynamicMethodInvocation
protected com.opensymphony.xwork2.config.Configuration
configuration
protected com.opensymphony.xwork2.ObjectFactory
objectFactory
Constructors Summary
public Form(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest request, HttpServletResponse response)


           
        super(stack, request, response);
    
Methods Summary
private voidevaluateClientSideJsEnablement(java.lang.String actionName, java.lang.String namespace, java.lang.String actionMethod)


        // Only evaluate if Client-Side js is to be enable when validate=true
        Boolean validate = (Boolean) getParameters().get("validate");
        if (validate != null && validate.booleanValue()) {

            addParameter("performValidation", Boolean.FALSE);

            RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration();
            ActionConfig actionConfig = runtimeConfiguration.getActionConfig(namespace, actionName);

            if (actionConfig != null) {
                List interceptors = actionConfig.getInterceptors();
                for (Iterator i = interceptors.iterator(); i.hasNext();) {
                    InterceptorMapping interceptorMapping = (InterceptorMapping) i.next();
                    if (ValidationInterceptor.class.isInstance(interceptorMapping.getInterceptor())) {
                        ValidationInterceptor validationInterceptor = (ValidationInterceptor) interceptorMapping.getInterceptor();

                        Set excludeMethods = validationInterceptor.getExcludeMethodsSet();
                        Set includeMethods = validationInterceptor.getIncludeMethodsSet();

                        if (MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, actionMethod)) {
                            addParameter("performValidation", Boolean.TRUE);
                        }
                        return;
                    }
                }
            }
        }
    
protected voidevaluateExtraParams()

        super.evaluateExtraParams();

        //boolean isAjax = "ajax".equalsIgnoreCase(this.theme);

        if (validate != null) {
            addParameter("validate", findValue(validate, Boolean.class));
        }

        // calculate the action and namespace
        /*String action = null;
        if (this.action != null) {
            // if it isn't specified, we'll make somethig up
            action = findString(this.action);
        }

        if (Dispatcher.getInstance().isPortletSupportActive() && PortletActionContext.isPortletRequest()) {
            evaluateExtraParamsPortletRequest(namespace, action);
        } else {
            String namespace = determineNamespace(this.namespace, getStack(),
                    request);
            evaluateExtraParamsServletRequest(action, namespace, isAjax);
        }*/

        if (onsubmit != null) {
            addParameter("onsubmit", findString(onsubmit));
        }

        if (target != null) {
            addParameter("target", findString(target));
        }

        if (enctype != null) {
            addParameter("enctype", findString(enctype));
        }

        if (method != null) {
            addParameter("method", findString(method));
        }

        if (acceptcharset != null) {
            addParameter("acceptcharset", findString(acceptcharset));
        }

        // keep a collection of the tag names for anything special the templates might want to do (such as pure client
        // side validation)
        if (!parameters.containsKey("tagNames")) {
            // we have this if check so we don't do this twice (on open and close of the template)
            addParameter("tagNames", new ArrayList());
        }
    
private voidevaluateExtraParamsPortletRequest(java.lang.String namespace, java.lang.String action)
Constructs the action url adapted to a portal environment.

param
action The action to create the URL for.


        if (this.action != null) {
            // if it isn't specified, we'll make somethig up
            action = findString(this.action);
        }

        String type = "action";
        if (TextUtils.stringSet(method)) {
            if ("GET".equalsIgnoreCase(method.trim())) {
                type = "render";
            }
        }
        if (action != null) {
            String result = PortletUrlHelper.buildUrl(action, namespace,
                    getParameters(), type, portletMode, windowState);
            addParameter("action", result);

            // namespace: cut out anything between the start and the last /
            int slash = result.lastIndexOf('/");
            if (slash != -1) {
                addParameter("namespace", result.substring(0, slash));
            } else {
                addParameter("namespace", "");
            }

            // name/id: cut out anything between / and . should be the id and
            // name
            if (id == null) {
                slash = action.lastIndexOf('/");
                int dot = action.indexOf('.", slash);
                if (dot != -1) {
                    id = action.substring(slash + 1, dot);
                } else {
                    id = action.substring(slash + 1);
                }
                addParameter("id", escape(id));
            }
        }

    
private voidevaluateExtraParamsServletRequest(java.lang.String action, java.lang.String namespace, boolean isAjax)

param
isAjax
param
namespace
param
action

        if (action == null) {
            // no action supplied? ok, then default to the current request (action or general URL)
            ActionInvocation ai = (ActionInvocation) getStack().getContext().get(ActionContext.ACTION_INVOCATION);
            if (ai != null) {
                action = ai.getProxy().getActionName();
                namespace = ai.getProxy().getNamespace();
            } else {
                // hmm, ok, we need to just assume the current URL cut down
                String uri = request.getRequestURI();
                action = uri.substring(uri.lastIndexOf('/"));
            }
        }

        String actionMethod = "";
        // FIXME: our implementation is flawed - the only concept of ! should be in DefaultActionMapper
        // handle "name!method" convention.
        if (enableDynamicMethodInvocation) {
            if (action.indexOf("!") != -1) {
                int endIdx = action.lastIndexOf("!");
                actionMethod = action.substring(endIdx + 1, action.length());
                action = action.substring(0, endIdx);
            }
        }

        final ActionConfig actionConfig = configuration.getRuntimeConfiguration().getActionConfig(namespace, action);
        String actionName = action;
        if (actionConfig != null) {

            ActionMapping mapping = new ActionMapping(action, namespace, actionMethod, parameters);
            String result = UrlHelper.buildUrl(actionMapper.getUriFromActionMapping(mapping), request, response, null);
            addParameter("action", result);

            // let's try to get the actual action class and name
            // this can be used for getting the list of validators
            addParameter("actionName", actionName);
            try {
                Class clazz = objectFactory.getClassInstance(actionConfig.getClassName());
                addParameter("actionClass", clazz);
            } catch (ClassNotFoundException e) {
                // this is OK, we'll just move on
            }

            addParameter("namespace", namespace);

            // if the name isn't specified, use the action name
            if (name == null) {
                addParameter("name", action);
            }

            // if the id isn't specified, use the action name
            if (id == null) {
                addParameter("id", action);
            }
        } else if (action != null) {
            // Since we can't find an action alias in the configuration, we just assume
            // the action attribute supplied is the path to be used as the uri this
            // form is submitting to.

            String result = UrlHelper.buildUrl(action, request, response, null);
            addParameter("action", result);

            // namespace: cut out anything between the start and the last /
            int slash = result.lastIndexOf('/");
            if (slash != -1) {
                addParameter("namespace", result.substring(0, slash));
            } else {
                addParameter("namespace", "");
            }

            // name/id: cut out anything between / and . should be the id and name
            if (id == null) {
                slash = result.lastIndexOf('/");
                int dot = result.indexOf('.", slash);
                if (dot != -1) {
                    id = result.substring(slash + 1, dot);
                } else {
                    id = result.substring(slash + 1);
                }
                addParameter("id", escape(id));
            }
        }

        // WW-1284
        // evaluate if client-side js is to be enabled. (if validation interceptor
        // does allow validation eg. method is not filtered out)
        evaluateClientSideJsEnablement(actionName, namespace, actionMethod);
    
protected booleanevaluateNameValue()

        return false;
    
public java.lang.StringgetDefaultOpenTemplate()

        return OPEN_TEMPLATE;
    
protected java.lang.StringgetDefaultTemplate()

        return TEMPLATE;
    
protected intgetSequence()
Get a incrementing sequence unique to this Form component. It is used by Form component's child that might need a sequence to make them unique.

return
int

        return sequence++;
    
public java.util.ListgetValidators(java.lang.String name)

        Class actionClass = (Class) getParameters().get("actionClass");
        if (actionClass == null) {
            return Collections.EMPTY_LIST;
        }

        List all = ActionValidatorManagerFactory.getInstance().getValidators(actionClass, (String) getParameters().get("actionName"));
        List validators = new ArrayList();
        for (Iterator iterator = all.iterator(); iterator.hasNext();) {
            Validator validator = (Validator) iterator.next();
            if (validator instanceof FieldValidator) {
                FieldValidator fieldValidator = (FieldValidator) validator;
                if (fieldValidator.getFieldName().equals(name)) {
                    validators.add(fieldValidator);
                }
            }
        }

        return validators;
    
protected voidpopulateComponentHtmlId(org.apache.struts2.components.Form form)
Form component determine the its HTML element id as follows:-
  1. if an 'id' attribute is specified.
  2. if an 'action' attribute is specified, it will be used as the id.

        boolean isAjax = "ajax".equalsIgnoreCase(this.theme);

        String action = null;
        if (this.action != null) {
            // if it isn't specified, we'll make somethig up
            action = findString(this.action);
        }

        if (id != null) {
            addParameter("id", escape(id));
        }
        if (Dispatcher.getInstance().isPortletSupportActive() && PortletActionContext.isPortletRequest()) {
            evaluateExtraParamsPortletRequest(namespace, action);
        } else {
            String namespace = determineNamespace(this.namespace, getStack(),
                    request);
            evaluateExtraParamsServletRequest(action, namespace, isAjax);
        }
    
public voidsetAcceptcharset(java.lang.String acceptcharset)

        this.acceptcharset = acceptcharset;
    
public voidsetAction(java.lang.String action)

        this.action = action;
    
public voidsetConfiguration(com.opensymphony.xwork2.config.Configuration configuration)

        this.configuration = configuration;
    
public voidsetEnableDynamicMethodInvocation(java.lang.String enable)

        enableDynamicMethodInvocation = "true".equals(enable);
    
public voidsetEnctype(java.lang.String enctype)

        this.enctype = enctype;
    
public voidsetMethod(java.lang.String method)

        this.method = method;
    
public voidsetNamespace(java.lang.String namespace)

        this.namespace = namespace;
    
public voidsetObjectFactory(com.opensymphony.xwork2.ObjectFactory objectFactory)

        this.objectFactory = objectFactory;
    
public voidsetOnsubmit(java.lang.String onsubmit)

        this.onsubmit = onsubmit;
    
public voidsetPortletMode(java.lang.String portletMode)

        this.portletMode = portletMode;
    
public voidsetTarget(java.lang.String target)

        this.target = target;
    
public voidsetValidate(java.lang.String validate)

        this.validate = validate;
    
public voidsetWindowState(java.lang.String windowState)

        this.windowState = windowState;