struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "bryan@xoscript.org" <br...@xoscript.org>
Subject Re: include result of another action in current action
Date Fri, 03 Feb 2006 15:44:22 GMT
Ok, I tested this this morning and it is working great. The idea here is to
add this action class to your config and set the url of the application tag to
the /ajax action. Your application class's method will get invoked and if it
returns an ActionForward the result of the path in that forward will be return
to your javascript call, otherwise you can just return and object.

This goes back to the discussion of adding the xoscript flavor of ajax to
struts, here is the quick view to the changes in the set up configuration
explained here.
http://www.xoscript.org/opencms/opencms/documentation/setupconfig.html

1. You would not need to declare the XSOServlet, the ActionServlet would be
all that is needed. You will need the xso.jar from the site above

2. Set the url of the application to the AjaxAction class below. and import
the main class for your application and optionally other imports if needed.

<xso:Application excludescript="true" 
servleturl="http://localhost:8080/webexit/ajax.do">
  <xso:Import name="main" object="com.yourcompany.app.Main"/>
</xso:Application>

3. Add the AjaxAction to your struts config.
<action path="/ajax" scope="sessioin"
type="com.yourcompany.struts.action.AjaxAction" validate="false"></action>

4. In your Main class you will have the option of returning an object of any
kind to the javascript call to it's methods or if you return an ActionForward
the action servlet will get the result of that path associated with that
action and return it to your javascript as a string.

public class Main {
    public ActionForward getTestPage() throws Exception{

        	ActionForward forward = new ActionForward();
        	forward.setPath("/test.jsp");
        	return forward;
    }
}

4. The javascript veriable main will now contain the resulting content of test.jsp

Here is the AjaxAciton class.

package com.yourcompany.struts.action;

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

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 org.xoscript.server.HttpMetaData;
import org.xoscript.server.HttpMetaRequestWraper;
import org.xoscript.server.HttpMetaResponseWraper;
import org.xoscript.service.LogFactory;
import org.xoscript.service.LogProvider;
import org.xoscript.service.ProxyObject;
import org.xoscript.service.ProxyObjectFactory;
import org.xoscript.service.XOService;
import org.xoscript.service.XOServiceFactory;

public class AjaxAction extends Action {

	private static LogProvider log = LogFactory.getLogProvider();
	private static XOService xoService = null;

    public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {
    	
    	ActionForward forward = null;

		if (log.isInfoEnabled()) {
			log.info("begin processRequest ------------------------------>: ");
		}

		try {
			/**
			 * The (POJO)'s method call invoked by xoService below
			 * can retrieve this meta data object from the
			 * XOServiceFactory to get information about the 
			 * request and response objects
			 */
			xoService = XOServiceFactory.getService();
			HttpMetaData metaData = new HttpMetaData();
			response.setContentType("text/xml; charset=ISO-8859-1");
			metaData.setRequest(request);
			metaData.setResponse(response);
			metaData.setContext(null);
			XOServiceFactory.setMetaData(metaData);
			
			/**
			 * The xml is read in and a ProxoyObject is created.
			 * The ProxyObject now contains the lookup information
			 * for the POJO and what method and argument should be
			 * invoked. The return value of the method is stored as
			 * an Object.
			 */
			Object o = xoService.invoke((ProxyObject) 
				ProxyObjectFactory.readProxyObject(request.getReader()));
			
			/**
			 * If the return value of the object is null. A Void class
			 * is put in it's place.
			 */
			if(o == null){
				o = Void.TYPE;
				if (log.isInfoEnabled()) {
					log.info("object was null returning void");
				}
			}
			
			if(o instanceof ActionForward){
				
				RequestDispatcher rd = 
					servlet.getServletContext().
					getRequestDispatcher(((ActionForward) o).getPath());
				
				HttpMetaRequestWraper req = metaData.getRequest();
				HttpMetaResponseWraper res = metaData.getResponse();
				rd.include(req,res);
				o = res.toString();
			}
			
			
			ProxyObjectFactory.writeProxyObject(response.getWriter(), o);

			metaData = null;
		} catch (Exception e) {
			try {
				/*
				 * In the event that an exception is thrown, the Throwable
				 * is wrapped in a ProxyObject and writen back to the 
				 * browser.
				 */
				ProxyObjectFactory.writeProxyObject(response.getWriter(), e);
				if (log.isDebugEnabled()) {
					log.debug(e);
				}
			} catch (Exception e1) {
				if (log.isDebugEnabled()) {
					log.debug(e1);
				}
			}
		}finally{
			XOServiceFactory.destroy();
		}
		
		return forward;
    }

}


Bryan LaPlante

---------- Original Message -----------
From: "bryan@xoscript.org" <bryan@xoscript.org>
To: "Struts Developers List" <dev@struts.apache.org>
Sent: Thu, 2 Feb 2006 20:04:37 -0500
Subject: include result of another action in current action

> Is it possible to include the result of another action from within your
> executing action. Next question, can I control the request and response used
> in the included action by passing wrappers to it?
> 
> Bryan LaPlante
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
------- End of Original Message -------

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


Mime
View raw message