struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sean Schofield <sean.schofi...@gmail.com>
Subject Re: commons-chain: joining with struts
Date Wed, 15 Dec 2004 18:18:26 GMT
Pedro,

A StrutsWebContext class might be an interesting idea.  If adopted, it
should go in the struts-chain project as opposed to commons-chain.  (I
am sending a copy of this response to the struts-dev mailing list
which is probably the best place to propose this kind of enhancement).

I'm not sure how useful it would be to have the ActionForm in your
commands but I don't work a lot on struts-chain so maybe others have
an opinion on that.

If you decide to go ahead with submitting an official patch (or just
using it in your own projects) the code should probably be modified to
take advantage of recent changes to commons-chain.

Specifically, you can now use CatalogFactory to get a copy of the
catalog you're interested in.  Just make sure to setup the
o.a.c.c.w.ChainListener with the appropriate parameters, and it will
configure the catalog when the webapp starts and it will automatically
be available to any java class using the same class loader (including
your proposed CommandAction).

Hope that helps,
sean


On Tue, 14 Dec 2004 15:56:54 +0100, Pedro Salgado <salgado.pc@04web.com> wrote:
> Hello,
> 
>  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.
> 
>  Regards,
> 
> 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()
>                .getServletContext().getAttribute("catalog");
>        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()
>                .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, 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
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message