FileDocCategorySizeDatePackage
ActionComponent.javaAPI DocExample11601Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

ActionComponent

public class ActionComponent extends Component

This tag enables developers to call actions directly from a JSP page by specifying the action name and an optional namespace. The body content of the tag is used to render the results from the Action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.

  • id (String) - the id (if specified) to put the action under stack's context.
  • name* (String) - name of the action to be executed (without the extension suffix eg. .action)
  • namespace (String) - default to the namespace where this action tag is invoked
  • executeResult (Boolean) - default is false. Decides wheather the result of this action is to be executed or not
  • ignoreContextParams (Boolean) - default to false. Decides wheather the request parameters are to be included when the action is invoked

public class ActionTagAction extends ActionSupport {

public String execute() throws Exception {
return "done";
}

public String doDefault() throws Exception {
ServletActionContext.getRequest().setAttribute("stringByAction", "This is a String put in by the action's doDefault()");
return "done";
}
}



....

success.jsp


success.jsp

....



The following action tag will execute result and include it in this page


The following action tag will do the same as above, but invokes method specialMethod in action


The following action tag will not execute result, but put a String in request scope under an id "stringByAction" which will be retrieved using property tag

Fields Summary
private static final Log
LOG
protected HttpServletResponse
res
protected HttpServletRequest
req
protected com.opensymphony.xwork2.ActionProxyFactory
actionProxyFactory
protected com.opensymphony.xwork2.ActionProxy
proxy
protected String
name
protected String
namespace
protected boolean
executeResult
protected boolean
ignoreContextParams
protected boolean
flush
Constructors Summary
public ActionComponent(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest req, HttpServletResponse res)


           
        super(stack);
        this.req = req;
        this.res = res;
    
Methods Summary
private java.util.MapcreateExtraContext()

        Map parentParams = null;

        if (!ignoreContextParams) {
            parentParams = new ActionContext(getStack().getContext()).getParameters();
        }

        Map newParams = (parentParams != null) ? new HashMap(parentParams) : new HashMap();

        if (parameters != null) {
            newParams.putAll(parameters);
        }

        ActionContext ctx = new ActionContext(stack.getContext());
        ServletContext servletContext = (ServletContext) ctx.get(ServletActionContext.SERVLET_CONTEXT);
        PageContext pageContext = (PageContext) ctx.get(ServletActionContext.PAGE_CONTEXT);
        Map session = ctx.getSession();
        Map application = ctx.getApplication();

        Dispatcher du = Dispatcher.getInstance();
        Map extraContext = du.createContextMap(new RequestMap(req),
                newParams,
                session,
                application,
                req,
                res,
                servletContext);

        ValueStack newStack = ValueStackFactory.getFactory().createValueStack(stack);
        extraContext.put(ActionContext.VALUE_STACK, newStack);

        // add page context, such that ServletDispatcherResult will do an include
        extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);

        return extraContext;
    
public booleanend(java.io.Writer writer, java.lang.String body)

        boolean end = super.end(writer, "", false);
        try {
            if (flush) {
                try {
                    writer.flush();
                } catch (IOException e) {
                    LOG.warn("error while trying to flush writer ", e);
                }
            }
            executeAction();

            if ((getId() != null) && (proxy != null)) {
                getStack().setValue("#attr['" + getId() + "']",
                        proxy.getAction());
            }
        } finally {
            popComponentStack();
        }
        return end;
    
private voidexecuteAction()
Execute the requested action. If no namespace is provided, we'll attempt to derive a namespace using buildNamespace(). The ActionProxy and the namespace will be saved into the instance variables proxy and namespace respectively.

see
org.apache.struts2.views.jsp.TagUtils#buildNamespace

        String actualName = findString(name, "name", "Action name is required. Example: updatePerson");

        if (actualName == null) {
            throw new StrutsException("Unable to find value for name " + name);
        }

        // handle "name!method" convention.
        final String actionName;
        final String methodName;

        int exclamation = actualName.lastIndexOf("!");
        if (exclamation != -1) {
            actionName = actualName.substring(0, exclamation);
            methodName = actualName.substring(exclamation + 1);
        } else {
            actionName = actualName;
            methodName = null;
        }

        String namespace;

        if (this.namespace == null) {
            namespace = TagUtils.buildNamespace(actionMapper, getStack(), req);
        } else {
            namespace = findString(this.namespace);
        }

        // get the old value stack from the request
        ValueStack stack = getStack();
        // execute at this point, after params have been set
        try {

            proxy = actionProxyFactory.createActionProxy(namespace, actionName, createExtraContext(), executeResult, true);
            if (null != methodName) {
                proxy.setMethod(methodName);
            }
            // set the new stack into the request for the taglib to use
            req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
            proxy.execute();

        } catch (Exception e) {
            String message = "Could not execute action: " + namespace + "/" + actualName;
            LOG.error(message, e);
        } finally {
            // set the old stack back on the request
            req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
        }

        if ((getId() != null) && (proxy != null)) {
            final Map context = stack.getContext();
            context.put(getId(), proxy.getAction());
        }
    
public com.opensymphony.xwork2.ActionProxygetProxy()

        return proxy;
    
public voidsetActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory actionProxyFactory)

param
actionProxyFactory the actionProxyFactory to set

        this.actionProxyFactory = actionProxyFactory;
    
public voidsetExecuteResult(boolean executeResult)

        this.executeResult = executeResult;
    
public voidsetFlush(boolean flush)

        this.flush = flush;
    
public voidsetIgnoreContextParams(boolean ignoreContextParams)

        this.ignoreContextParams = ignoreContextParams;
    
public voidsetName(java.lang.String name)

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

        this.namespace = namespace;