Thinking on hiring me?

Please read

Fernando Guillén

a Freelance Web Developer

cabecera decorativa

software development as an artistic expression

Archive for the ‘struts’ Category

Jueves, Julio 26th, 2007

Struts: cargando un ActionForm desde atributos en la ‘Request’

Struts tiene pegas, tiene grandes virtudes que hace que sigamos amándolo y respetándolo… hasta que Spring nos separe, pero tiene grandes pequeñas pegas.

Una de ellas es que cuando ‘un caso de uso’ requiere de uno o varios parámetros para iniciar la secuencia siempre se esperan como parámetros de la petición al estilo:

http://miaplicacion.com/peticion.do?parametro=valor

Mismamente el detalle de un registro o de un objeto cuya id se la pasamos por parámetro de la petición:

http://miaplicacion.com/detalle.do?id=1

Entonces nuestro Action recibe la petición y automáticamente carga el ActionForm asociado a partir de los parámetros, en este caso ‘id’.

Pero ¿qué pasa si quiero invocar a este ‘caso de uso’ desde otro Action al estilo?:

<forward name="exito" path="/detalle.do" />

Pues que no hay manera de pasarle el parámetro id a ese Action y el ActionForm no se cargará.

La solución más rápido es hacer que el Action sea ambidiestro y sea capaz de recibir valores tanto desde parámetros como desde un atributo de la ‘Request’ de tal manera que el Action origen que invoca a este Action destino cargue el valor que le quiere pasar en un atributo de la ‘Request’.

Esta solución tiene dos problemas:

* Se genera un montón de código basura en el inicio del Action para proporcionarle la capacidad de recoger valores desde las dos fuentes.

* No aprovechamos la funcionalidad de Struts para gestionar la auto-carga de valores.

* No se puede validar en formulario pues si enviamos los datos en los atributos de la Request el formulario ni se entera.

Entonces debemos buscar una solución más sólida, ágil y completamente reutilizable.

El RequestForm

Se trata de un ActionForm con la cualidad de buscar entre la Request actual atributos que coincidan con alguna de sus propiedades. Si el ActionForm encuentra el atributo con el mismo nombre que una de sus propiedades y también tiene un método ’set’ que recibe un objeto del mismo tipo que el valor contenido en el atributo de la Request entonces lo invoca y así sucesivamente hasta que el ActionForm se autorrellena.

Toda esta funcionalidad está encerrada en un método del RequestForm, pero este método no se invoca automáticamente así que hay que invocarlo a mano y hay que buscar un buen momento para hacerlo. Si miramos la secuencia con que Struts invoca a los métodos del ActionForm vemos que el mejor momento para invocar la carga de propiedades desde la Request es:

* Justo después del reset(). Los valores cargados desde la Request serán machacados con los valores que se encuentren entre los parámetros de la petición.

* Justo antes del validate(). Los valores cargados desde los parámetros de la petición serán sobrescritos por los valores encontrados en la Request.

Este es el ActionForm que se precarga rebuscando en la Request:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.constela.tronco.util.StringUtilities;
import com.us3.util.Util;

/**
 * <p>
 * </p>
 *
 * @internal_author fguillen@constelanetworks.com
 * @date 25/07/2007
 *
 */
public class RequestForm extends ActionForm {

	/**
	 * <p>
	 * This override for reset method invokes to chargeFromRequest
	 * this is the best place to invoke this method.
	 * </p>
	 * @date: 25/07/2007
	 * @author: fguillen@constelanetworks.com
	 *
	 * @param arg0
	 * @param arg1
	 */
	@Override
	public void reset(
		ActionMapping mapping,
		HttpServletRequest request
	) {

		super.reset( mapping, request );

		try {

			this.chargeFromRequest( request );

		}	catch ( Exception e ) {
			throw new Error( e );
		}

	}

	/**
	 * <p>
	 * Method that allows to the ActionForm to
	 * charge its fields from the Request's attributes
	 * </p>
	 *
	 * <p>
	 * The method will look for the fields on the ActionForm
	 * and will look for attributes on Request with the same name
	 * and will look for 'set' method on the ActionForm. If there exist
	 * both Request's attribute and 'set' method the 'set' method is invoked
	 * with the Request's attribute's value.
	 * </p>
	 *
	 * @date: 25/07/2007
	 * @author: fguillen@constelanetworks.com
	 *
	 * @param request
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public void chargeFromRequest(
		HttpServletRequest request
	) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{

		//
		// the fields of the ActionForm
		//
		Class c = this.getClass();
		Field[] fields = c.getDeclaredFields();

		//
		// walk throw the array of fields looking
		// for attributes on request with same name
		//
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];

			String fieldName = field.getName();
			Object attributeValue =
				request.getAttribute( fieldName );			

			//
			// if attributeValue is not null
			// and the ActionForm has a set method
			// try to asign value to a field
			//
			if( attributeValue != null ){
				//
				// has it a public set method?
				//
				String methodSet =
					"set" +
					StringUtilities.capitalizeWord( fieldName );

				Method method = null;
				try{
					method =
						c.getDeclaredMethod(
							methodSet,
							new Class[] { attributeValue.getClass() }
						);
				} catch ( Exception e ) {
					// the method does not exists
				}

				if( method != null ){
					method.invoke(
						this,
						new Object[] { attributeValue }
					);
				}
			}
		}
	}
}

Entonces ahora, cada vez que queramos que un ActionForm sea capaz de precargarse desde la ‘Request’ debemos heredar de nuestro RequestForm y no directamente de ActionForm. Hay que acordarse que si sobrescribimos el método reset() en nuestra implementación hay que hacer una llamada a super.reset() al finalizar nuestras sentencias de reseteo.

a Freelance Web Developer is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

Creative Commons License
Fernando Guillen's blog by Fernando Guillen is licensed under a Creative Commons Attribution-NoDerivs 3.0 Unported License.