⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actionhandler.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
package org.jahia.utils;import java.lang.reflect.*;import java.util.*;import java.io.*;import javax.servlet.http.*;/** * Class ActionHandler: determines the method to invoke for an Application * (typically a Web Application), given an action. * * @author  Stephane Boury, Jerome Tamiotti * @version 1.1 */public class ActionHandler {        /* The class on which to call the methods */    private Class actionClass;         /** 	 * An instance of the class on which to call the methods	 * This instance is null here because the class is static,	 * and the parameter of 'invokeMethod' method will be ignored	 */    private Object instanceOfClass = null;        /* The hashtable containing the method names */	private Properties methods;        /**	 * Class constructor: builds the hashtable by reading a flat file.	 *	 * @param propsFile     the full path to the file containing pairs of	 * "action - method name"	 * @param theClass      the name of the Class on which to call the methods	 */    public ActionHandler(String propsFile, String theClass) {        methods = new Properties();		try {			FileInputStream fis = new FileInputStream(propsFile);			methods.load(fis);			fis.close();		}		catch (IOException e) {			JahiaConsole.println("ActionHandler-ActionHandler", e.toString());		}                try {            actionClass = Class.forName(theClass);        } catch (ClassNotFoundException ex) {            JahiaConsole.println("ActionHandler-ActionHandler", ex.toString());        } catch (Exception ex) {            JahiaConsole.println("ActionHandler-ActionHandler", ex.toString());        }    }        /**     * Calls the appropriate method given the action.     *     * @param action    the action that triggers the method to call     * @param request   the request parameter for the method     */            public Object call(String action, HttpServletRequest request) throws NullPointerException {                String theMethod = methods.getProperty(action);		if (theMethod == null) { //method not found in list			JahiaConsole.println("ActionHandler-call","No method found for action " + action);			throw new NullPointerException();		}        try {			Class theParams[] = {Class.forName("javax.servlet.http.HttpServletRequest")};            Method thisMethod = actionClass.getDeclaredMethod(theMethod, theParams);			Object args[] = {request};            return thisMethod.invoke(instanceOfClass, args);        } catch(ClassNotFoundException cnfe) {            JahiaConsole.println("ActionHandler-call", cnfe.toString());			throw new NullPointerException();		} catch(NoSuchMethodException nsme) {            JahiaConsole.println("ActionHandler-call", nsme.toString());			throw new NullPointerException();		} catch(IllegalAccessException iae) {            JahiaConsole.println("ActionHandler-call", iae.toString());			throw new NullPointerException();		} catch(InvocationTargetException ite) {            JahiaConsole.println("ActionHandler-call", ite.toString());			throw new NullPointerException();		}     }            /**     * Calls the appropriate method given the action.     *     * @param action     the action that triggers the method to call     * @param request    the request parameter for the method     * @param response   the response parameter for the method     */            public Object call( Object instanceOfClass, String action, HttpServletRequest request, HttpServletResponse response) throws NullPointerException {                String theMethod = methods.getProperty(action);		if (theMethod == null) { //method not found in list			JahiaConsole.println("ActionHandler-call","No method found for action " + action);			throw new NullPointerException();		}        try {			Class theParams[] = {Class.forName("javax.servlet.http.HttpServletRequest"),Class.forName("javax.servlet.http.HttpServletResponse")};            Method thisMethod = actionClass.getDeclaredMethod(theMethod, theParams);			Object args[] = {request,response};            return thisMethod.invoke(instanceOfClass, args);        } catch(ClassNotFoundException cnfe) {            JahiaConsole.printe("ActionHandler-call", cnfe);			throw new NullPointerException();		} catch(NoSuchMethodException nsme) {            JahiaConsole.printe("ActionHandler-call", nsme);			throw new NullPointerException();		} catch(IllegalAccessException iae) {            JahiaConsole.printe("ActionHandler-call", iae);			throw new NullPointerException();		} catch(InvocationTargetException ite) {            JahiaConsole.printe("ActionHandler-call", ite);			throw new NullPointerException();		}     }        }  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -