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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]