commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pedro Salgado <>
Subject commons-chain: joining with struts
Date Tue, 14 Dec 2004 14:56:54 GMT

  I have read the commons-chain cookbook and I think there is a better way
to join with struts.

  In a command, one can only retrieve the ActionForm by codding
request/session.getAttribute("formName"); through the ServletWebContext.

  Wouldn't it be better if there was a StrutsWebContext class with a
getActionForm() method that automatically gave the form? This way the
command should not have to know what is the name of the form (Bor-ring
;D)... only the needed form properties.

  I think this would simplify a little bit. All comments are welcome.


Pedro Salgado

/* My Struts Web Context */
package test;

import org.apache.commons.chain.web.servlet.ServletWebContext;
import org.apache.struts.action.ActionForm;

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

 * //TODO [JAVADOC] class description
 * @version {version} [{date}]
public class StrutsWebContext extends ServletWebContext {

    private ActionForm form;
    public StrutsWebContext(ServletContext context,
                            HttpServletRequest request,
                            HttpServletResponse response,
                            ActionForm form) {

        super(context, request, response);
        this.form = form;
    public ActionForm getActionForm() {
        return form;

/* Struts chain base action. */

package test;

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.web.servlet.ServletWebContext;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

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

public class CommandAction extends Action {

    private static final String SUCCESS = "success";

    protected final Command getCommand(final ActionMapping mapping,
                                       final HttpServletRequest request)
            throws Exception {

        Catalog catalog = (Catalog) request.getSession()
        String name = mapping.getName();
        Command command = catalog.getCommand(name);

        return command;

    protected final Context getContext(final ActionMapping mapping,
                                 final HttpServletRequest request,
                                 final HttpServletResponse response,
                                 final ActionForm form)
            throws Exception {

        ServletContext application = request.getSession()
        return new StrutsWebContext(application, request, response, form);

    protected final ActionForward findLocation(final ActionMapping mapping,
                                               final boolean stop) {

        if (stop) return mapping.getInputForward(); // Something failed

        return mapping.findForward(SUCCESS);

    public final ActionForward execute(final ActionMapping mapping,
                                       final ActionForm form,
                                       final HttpServletRequest request,
                                       final HttpServletResponse response)
            throws Exception {

        Command command = getCommand(mapping, request);
        Context context = getContext(mapping, request, response);

        boolean stop = command.execute(context);
        ActionForward location = findLocation(mapping, stop);

        return location;


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message