Sorry but HTML mail is not easy with notepad.exe ;)


What do you about event programming in Java Server Side programming (using java.lang.reflect ?

With this html button :

<input type="submit" name="ok" value="ajouter"> invoke method onAjouter(,,,)
<input type="submit" name="ok" value="enregistrer"> invoke method onEnregistrer(,,,)

Etc...


package com.csi.controller;

// Struts
import org.apache.struts.action.*;
// Servlet
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Import log4j classes.
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
// Reflect
import java.lang.reflect.Method;


/**
* Titre : MyAction.java
* Description : Event Programming in Struts ;)
* Copyright : Copyright (c) 2001
* @author : b7st@yahoo.fr
* @version 0.00001a
*/
public abstract class MyAction extends Action {
static Category log = Category.getInstance(MeeschaertAction.class);

public MyAction () {
}

public ActionForward perform (ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {

// Get submit parameter 'ok' value
// ex: <input type="submit" name="ok" value="ajouter">
String button = request.getParameter("ok");
if (button != null) {
// If ok, get the good method name
// ex: onAjouter
String methodName = "on" + button.toUpperCase().substring(0, 1)
+ button.toLowerCase().substring(1, button.length());
Method method = null;
Class thisAction = this.getClass();
Class param[] = new Class[4];
param[0] = ActionMapping.class;
param[1] = ActionForm.class;
param[2] = HttpServletRequest.class;
param[3] = HttpServletResponse.class;
try {
// Test if the method exist in derived class
method = thisAction.getDeclaredMethod(methodName, param);
Object value[] = new Object[4];
value[0] = mapping;
value[1] = form;
value[2] = request;
value[3] = response;
// Yes, so invoke it !
return (ActionForward)method.invoke(this, value);
} catch (NoSuchMethodException e) {
log.debug("EVT(" + e + "): No " + methodName + "(,,,) found, using go(,,,)");
} catch (Exception e) {
log.warn("EVT(" + e + "): No " + methodName + "(,,,) found, using go(,,,)",
e);
}
}
// Else invoke normal method
return go(mapping, form, request, response);
}

public abstract ActionForward go (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws java.io.IOException,
javax.servlet.ServletException;

}


Emmanuel,



Do You Yahoo!?
Yahoo! Courrier Un e-mail gratuit @yahoo.fr !