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

📄 actionservlet.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

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

import jodd.util.StringUtil;

/**
 * Replacement and enhancement of <code>HttpServlet</code>, containing additional
 * functionalities. Must be inherited by user.
 * <p>
 *
 * <code>ActionServlet</code> may be used in two ways: 1) as standard http
 * serlvet, or 2) as an action servlet in mvc2 framework.
 * <p>
 *
 * <h2>1) <code>ActionServlet</code> as http servlet</h2>
 *
 * Here both POST and GET requests calls <code>doRequest</code> method, which
 * is the entering point of the servlet. For more easier work, this servlet
 * may use one of the helper methods. Helper methods can be grouped in 3
 * groups:
 *
 * <ul>
 *
 * <li>invokers - they execute an action. Action may be specified in request
 * parameter 'action' or internal, during method invocation. Actions are
 * (names of the) methods in the same <code>ActionServlet</code>
 * implementation. Action methods can help in reducing the number of
 * <code>ActionServlet</code> classes, since one class may be used for
 * handling more than one request.</li>
 *
 * <li>forwarders - perform forwards to specific url. They also can read
 * forward destination from the request parameter 'forward'. </ul>
 *
 * <li>redirectors - perform redirect to specific url. They also can read
 * redirect destination from the request parameter 'redirect'. </ul>
 * </ul>
 *
 * Default forwarders and redirector names given as request parameter are
 * either 'forward' or 'redirect' respectively. If more than one parameter
 * is needed, they have to be named as 'forward-...' or 'redirect-...'.
 *
 * <h2>2) <code>ActionServlet</code> as framework action</h2>
 * <h3>using default handler</h3>
 *
 * In this use case, on the client request container will invoke the
 * controller (<code>ActionController</code>). Controller is configured from
 * a XML file. Here, controller always invokes <code>doAction()</code> method
 * (instead of <code>doRequest</code>). This method must return one of the
 * forwards parameters specified in the XML configuration file, type of
 * String. Afterm Controller will pass the control to adequate URI, as
 * specified in the configuration file.
 * <p>
 *
 * It is obvious that forwarders and redirectors should not be used now. On
 * the other hand, invokers can be freely used. Moreover, they are just a bit
 * more suited for this kind of usage, since whatever is returned from the
 * invoker is converted to String so it may be passed back to the controller.
 *
 * <h3>using mapped methods</h3>
 * An action can be directly mapped to an method inside ActionServlet class.
 * Method should return an string that represent forwarding. All this is
 * set in the configuration file. In this use case, there is no more need to
 * use invokers, since invoking is done automatically by the Controller.
 * <p>
 *
 * So, in this case there is no need to overload and use any of the
 * <code>ActionServlet</code> methods.
 *
 * <h3>using action parameters</h3>
 * Action can use specific parameters as defined in configuration xml file.
 * This may be handy, for example, when one ActionServlet is mapped to many
 * requests and when it should act a bit differently for some groups of
 * request. Anyhow, parameters may be used in different ways. Moreover, each
 * action can retrieve action path or some of ActionData values of the
 * current request.
 */
public abstract class ActionServlet extends HttpServlet {

	public static String parameterActionName = "action";
	public static String parameterForwardName = "forward";
	public static String parameterRedirectName = "redirect";


	// ---------------------------------------------------------------- controler

	ActionController controller = null;

	/**
	 * Returns action controller that created this ActionServlet object.
	 * It should be used for system usage.
	 *
	 * @return action controller instance
	 */
	public ActionController getController() {
		return controller;
	}

	// ---------------------------------------------------------------- doXXX() methods
	
	public final static int METHOD_UNKNOWN = 0;
	public final static int METHOD_GET = 1;
	public final static int METHOD_POST = 2;
	
	private int method = METHOD_UNKNOWN;
	
	
	/**
	 * Returns method (POST/GET) which has been used for invoking the action.
	 * 
	 * @return method (POST/GET) which has been used for invoking the action
	 */
	public final int getMethod() {
		return method;
	}
	
	/**
	 * Default doGet method, calls doRequest(). Should not be overloaded.
	 *
	 * @param request
	 * @param response
	 *
	 * @exception IOException
	 * @exception ServletException
	 * @see #doRequest
	 */
	protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		method = METHOD_GET;
		doRequest(request, response);
	}

	/**
	 * Default doGet method, calls doRequest(). Should not be overloaded.
	 *
	 * @param request
	 * @param response
	 *
	 * @exception IOException
	 * @exception ServletException
	 * @see #doRequest
	 */
	protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		method = METHOD_POST;
		doRequest(request, response);
	}

	/**
	 * Main Get/Post handler.
	 *
	 * @param request
	 * @param response
	 *
	 * @exception IOException
	 * @exception ServletException
	 * @see #doAction
	 */
	public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
	}

	/**
	 * When <code>ActionController</code> and MVC2 is used,
	 * <code>doRequest()</code> can not be used as servlet entry point. Instead,
	 * controller will invoke this method. The only difference from the
	 * <code>doRequest()</code> is that this method must return a String that
	 * represents a 'mapping' where to forward/redirect, i.e. forwarding and
	 * redirections are done by controler, not by servlet. These mappings are
	 * defined in 'actions.xml' external file.
	 *
	 * @param request
	 * @param response
	 *
	 * @return mapping string
	 * @exception IOException
	 * @exception ServletException
	 * @see #doRequest
	 */
	public String doAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		return null;
	}

	// ---------------------------------------------------------------- invokers

	/**
	 * Calls a method defined in the request paramenter. Method name is read from
	 * the request. If method name is null (doesn't exist in the request) nothing
	 * happens.
	 *
	 * @param request
	 * @param response
	 *
	 * @return true if action was invoked, otherwise false
	 * @exception IOException
	 * @exception ServletException
	 */
	public String invokeAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String actionName = request.getParameter(parameterActionName);
		return invokeAction(request, response, actionName);
	}

	/**
	 * Invoke a method from this ActionServlet class. Methods name is given as
	 * parameter. If method name is null nothing happens.
	 *
	 * @param request
	 * @param response
	 * @param actionName name of the method to invoke
	 *
	 * @return null if error, otherwise string mapping
	 * @exception IOException
	 * @exception ServletException
	 */
	public String invokeAction(HttpServletRequest request, HttpServletResponse response, String actionName) throws IOException, ServletException {
		if (actionName == null) {
			return null;
		}
		try {
			Method m = this.getClass().getMethod(actionName, new Class[] {HttpServletRequest.class, HttpServletResponse.class});
			Object result = m.invoke(this, new Object[] {request, response});
			return StringUtil.toString(result);
		} catch (Exception e) {
		}
		return null;
	}


	/**
	 * Invokes external action that is defined in other ActionServlet class and
	 * not necessary in current one. It simply delegates call to
	 * <code>ActionController.invokeAction()</code>.
	 *
	 * @param request    http request
	 * @param response   http response
	 * @param actionPath action path as defined in configuration xml file
	 *
	 * @return external action forward string, or ACTION_NOT_FOUND if not founded
	 * @exception IOException
	 * @exception ServletException
	 * @see ActionController#invokeAction(HttpServletRequest, HttpServletResponse, String)
	 */
	public String invokeExternalAction(HttpServletRequest request, HttpServletResponse response, String actionPath) throws IOException, ServletException {
		return controller.invokeAction(request, response, actionPath);
	}

	// ---------------------------------------------------------------- forward

	/**
	 * Performs forward with use of the RequestDispatcher.
	 *
	 * @param request
	 * @param response
	 * @param url      URL where to forward
	 *
	 * @exception IOException
	 * @exception ServletException
	 */
	protected boolean forward(HttpServletRequest request, HttpServletResponse response, String url) throws IOException, ServletException {
		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		if (dispatcher != null) {
			dispatcher.forward(request, response);
			return true;
		}
		return false;
	}

	/**
	 * Performs forward with use of the RequestDispatcher.
	 * URL is read from the request. If URL doesn't exist, nothing happens.
	 *
	 * @param request
	 * @param response
	 *
	 * @return true if parameter found, false otherwise
	 * @exception IOException
	 * @exception ServletException
	 */
	protected boolean forwardParam(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		return forwardParam(request, response, null);
	}

	/**
	 * Performs forward where URL is read from the request. If URL doesn't exist,
	 * nothing happens.
	 *
	 * @param request  http request
	 * @param response http response
	 * @param s        forward parameters suffix, added to the default parameter name
	 *
	 * @return <code>true</code> if parameter found and forward was sucessful,
	 *         <code>false</code> otherwise
	 * @exception IOException
	 * @exception ServletException
	 * @see #forward
	 */
	protected boolean forwardParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException, ServletException {
		String paramName = parameterForwardName;
		if ((s != null) && (s.equals("") == false)) {
			paramName += "-" + s;
		}
		String url = request.getParameter(paramName);
		if (url != null) {
			return forward(request, response, url);
		}
		return false;
	}

	// ---------------------------------------------------------------- redirect

	/**
	 * Performs redirection.
	 *
	 * @param request
	 * @param response
	 * @param url      URL where to redirect
	 *
	 * @exception IOException
	 * @exception ServletException
	 */
	protected void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
		if (url.startsWith("/") == true) {
			url = request.getContextPath() + url;
		}
		response.sendRedirect(url);
	}

	/**
	 * Performs redirection. URL is read from the request. If URL doesn't exist,
	 * nothing happens.
	 *
	 * @param request
	 * @param response
	 *
	 * @return true if parameter found, false otherwise
	 * @exception IOException
	 * @exception ServletException
	 */
	protected boolean redirectParam(HttpServletRequest request, HttpServletResponse response)  throws IOException {
		return redirectParam(request, response, null);
	}

	/**
	 * Performs redirection where URL is read from the request. If URL doesn't
	 * exist, nothing happens.
	 *
	 * @param request
	 * @param response
	 * @param s        redirect parameters suffix, added to the default parameter name
	 *
	 * @return true if parameter found, false otherwise
	 * @exception IOException
	 * @exception ServletException
	 * @see #redirect
	 */
	protected boolean redirectParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException {
		String paramName = parameterRedirectName;
		if ((s != null) && (s.equals("") == false)) {
			paramName += "-" + s;
		}
		String url = request.getParameter(paramName);
		if (url != null) {
			redirect(request, response, url);
			return true;
		}
		return false;
	}


	// ----------------------------------------------------------------	action data

	/**
	 * Returns action parameter value.
	 *
	 * @param request request
	 * @param name    parameter name
	 *
	 * @return action parameter value of <code>null</code> if parameter has not been defined.
	 * @see jodd.servlet.ActionData
	 */
	public String getActionParameter(HttpServletRequest request, String name) {
		ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
		return actionData.getParameter(name);
	}


	/**
	 * Returns path of mapped action forward.
	 *
	 * @param request request
	 * @param name    forward name
	 *
	 * @return forward path
	 * @see jodd.servlet.ActionData
	 */
	public String getActionForwardPath(HttpServletRequest request, String name) {
		ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
		return actionData.getForwardPath(name);
	}

	/**
	 * Returns mapped action method name.
	 *
	 * @param request request
	 *
	 * @return mapped action mathod name
	 * @see jodd.servlet.ActionData
	 */
	public String getActionMethodName(HttpServletRequest request) {
		ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
		return actionData.getMethod();
	}


	/**
	 * Returns mapped action type.
	 *
	 * @param request request
	 *
	 * @return mapped action type
	 * @see jodd.servlet.ActionData
	 */
	public String getActionType(HttpServletRequest request) {
		ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
		return actionData.getType();
	}
	
	/**
	 * Returns <code>true</code> if specified forward is actually a redirect.
	 *
	 * @param request request
	 * @param name    forward name
	 *
	 * @return <code>true</code> if specified forward is actually a redirect, <code>false</code> otherwise
	 * @see jodd.servlet.ActionData
	 */
	public boolean isActionForwardRedirect(HttpServletRequest request, String name) {
		ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
		return actionData.isForwardRedirect(name);
	}


	/**
	 * Returns action path of a current request.
	 *
	 * @param request request
	 *
	 * @return action path
	 * @see jodd.servlet.ActionData
	 */
	public String getActionPath(HttpServletRequest request) {
		return (String) request.getAttribute(ActionController.ACTION_PATH);
	}


}

⌨️ 快捷键说明

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