What do you about event programming in Java Server
Side programming ?
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...
<font size="1" color="#000000" face="Arial, Helvetica,
sans-serif">
package com.csi.controller;
<b>// Struts</b>
import org.apache.struts.action.*;
<b>// Servlet</b>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
<b>// Import log4j classes.</b>
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
<b>// Reflect</b>
import java.lang.reflect.Method;
<b>/**</b>
<b> * Titre : MyAction.java</b>
<b> * Description : Event Programming in Struts ;)</b>
<b> * Copyright : Copyright (c) 2001</b>
<b> * @author : b7st@yahoo.fr</b>
<b> * @version 0.00001a</b>
<b> */
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 {
<b>// Get submit parameter 'ok' value</b>
<b>// ex: <input type="submit" name="ok"
value="ajouter"></b>
String button = request.getParameter("ok");
if (button != null) {
<b>// If ok, get the good method name </b>
<b>// ex: onAjouter</b>
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 {
<b>// Test if the method exist in
derived class</b>
method =
thisAction.getDeclaredMethod(methodName, param);
Object value[] = new Object[4];
value[0] = mapping;
value[1] = form;
value[2] = request;
value[3] = response;
<b>// Yes, so invoke it !</b>
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);
}
}
<b>// Else invoke normal method</b>
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;
}
</font>
Emmanuel,
___________________________________________________________
Do You Yahoo!? -- Un e-mail gratuit @yahoo.fr !
Yahoo! Courrier : http://fr.mail.yahoo.com
|