commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <>
Subject [Jakarta-commons Wiki] Update of "CommonsChainAndSpringFramework" by MarcoMistroni
Date Tue, 12 Apr 2005 17:52:10 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by MarcoMistroni:

New page:
{{{Below are the steps of a possble solution for using commons-chain with Struts, having the
Catalog configured via Spring.

Write an implementation of org.apache.commons.chain.Catalog interface which must implement
also org.springframework.context.ApplicationContextAware in order to retrieve commands from
spring configuration file.
Below is a possible implementation 

public class SpringCatalog implements Catalog,ApplicationContextAware {
	 private ApplicationContext appContext;
	 public SpringCatalog() {
		 System.out.println("Instantiating Spring Catalog..");
	 public void addCommand(String name, Command command) {
		 // does nothing here..
	 public Command getCommand(String name) {
		 return (Command)appContext.getBean(name);

       public Iterator getNames() {
		  Map commands = new HashMap();
		  try {
			  commands = appContext.getBeansOfType(
		  } catch(Exception e) {
			  System.err.println("Error in retrieving command names..");
		  return commands.keySet().iterator();
	 public void setApplicationContext(ApplicationContext ctx)
	  	throws ApplicationContextException, BeansException {
		  appContext = ctx;

* Declare your Catalog class and commands in your spring configuration file (usually it is

<?xml version="1.0" encoding="UTF-8"?>
	<!-- ========================= GENERAL DEFINITIONS ========================= -->


	<!-- Configurer that replaces ${...} placeholders with values from properties files -->
	<!-- (in this case, mail and JDBC related properties) -->

     <bean id="persistenceManager" class="com.myapp.strutschain.bean.PersistenceManager"/>
     <bean name="catalog" class="com.myapp.strutschain.core.SpringCatalog"/>
     <bean name="login" class="org.apache.commons.chain.impl.ChainBase">
                                <ref bean="firstCmd"/>
                                <ref bean="secondCmd"/>
     <bean name="firstCmd"
     <bean name="secondCmd"
		<property name="persistenceManager">
			<ref local="persistenceManager"/>

* Declare the Spring plugIn in your struts configuration file

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"

* Write an Action class servers as base to retrieve commands

public class CommandAction extends ActionSupport {
    protected static String SUCCESS = "success";

    protected Command getCommand(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        Catalog catalog = (Catalog) getWebApplicationContext().getBean("catalog");
	 String name = mapping.getName();
	 System.out.println("Retrieving command for action"+ name);
        Command command = catalog.getCommand(name);
        return command;

    protected Context getContext(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        ServletContext application = request.getSession()
        Context context = new ServletWebContext(
                application, request, response);
        return context;
    protected ActionForward findLocation(ActionMapping mapping,
            boolean stop) {
        if (stop) return mapping.getInputForward(); // Something failed
        return mapping.findForward(SUCCESS);

    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        Command command = getCommand(mapping, form, request, response);
        Context context = getContext(mapping, form, 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