commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pedro Salgado <salgado...@04web.com>
Subject Re: commons-chain: joining with struts
Date Tue, 14 Dec 2004 15:03:07 GMT

Sorry the command action needed some adjustments (below is the correct
version).

Pedro Salgado

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()
                .getServletContext().getAttribute("catalog");
        String name = mapping.getName();
        Command command = catalog.getCommand(name);

        return command;
    }

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

        ServletContext application = request.getSession()
                .getServletContext();
        
        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, form, request, response);

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

        return location;
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message