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

📄 handlecrudaction.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.servlet.action;import java.lang.reflect.Method;import java.util.Collections;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import net.sf.irunninglog.service.ICRUDService;import net.sf.irunninglog.servlet.RequestProcessor;import net.sf.irunninglog.servlet.UserContainer;import net.sf.irunninglog.servlet.formbean.ValueBean;import net.sf.irunninglog.util.ConstantValues;import net.sf.irunninglog.util.DTO;import net.sf.irunninglog.util.Utilities;/** * Action used to handle CRUD (create, read, update, delete) requests. * This class will use the <code>UserContainer</code>'s handle to a * CRUD service to perform any requests, update the appropriate object(s), * deal with any errors, and forward control to the appropriate resource. * * <p/> * * In addition to the 4 CRUD actions, this class can handle actions where * either a create or an update action should be performed.  If an action of * 'save' is received, this class will use the value bean's * value specified in the mapping's parameter to decide whether to perform a * create or an update.  Additionally, specifying a request parameter with a * key of 'delete' will lead to the execution of a delete action. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.3 $ $Date: 2005/06/25 15:18:20 $ * @since iRunningLog 1.0 */public final class HandleCRUDAction extends BaseAction {    /** <code>Log</code> instance for this class. */    private static final Log LOG = LogFactory.getLog(HandleCRUDAction.class);    /** Key for the request parameter used to trigger a delete action. */    private static final String DELETE_PARAMETER = "delete";    /** Name of the method used to perform a 'create' action. */    private static final String ACTION_CREATE = "create";    /** Name of the method used to perform a 'save' action. */    private static final String ACTION_SAVE = "save";    /** Name of the method used to perform a 'update' action. */    private static final String ACTION_UPDATE = "update";    /** String used to delimit the mapping parameter's tokens. */    private static final String DELIMETER = ".";    /** Cache for methods used to service requests. */    private static final Map METHOD_CACHE;    static {        // Cache the methods used to make service calls        METHOD_CACHE = Collections.synchronizedMap(new HashMap());        Method [] methods = ICRUDService.class.getMethods();        for (int i = 0; i < methods.length; i++) {            METHOD_CACHE.put(methods[i].getName(), methods[i]);        }    }    /**     * Perform the CRUD operation.  Return the correct forward based     * on whether or not any validation errors were encountered.     *     * @param mapping The <code>ActionMapping</code> used to select this     *                instance.  The parameter must contain the action to be     *                performed     * @param form The optional <code>ActionForm</code> bean for this request     * @param request The HTTP request being processed     * @param response The HTTP response being processed     * @param container The <code>UserContainer</code> object for the current     *                  user     * @return The forward describing where/how to forward control     * @exception Exception if the application business logic throws     *            an exception     */    public ActionForward executeAction(ActionMapping mapping,                                       ActionForm form,                                       HttpServletRequest request,                                       HttpServletResponse response,                                       UserContainer container)                                                             throws Exception {        ValueBean bean = (ValueBean) form;        String methodName = mapping.getParameter();        Method method = getMethod(methodName, bean, request);        if (LOG.isDebugEnabled()) {            LOG.debug("executeAction: Executing action '" + method.getName()                      + "' on bean " + bean);        }        DTO valueObject = (DTO) method.invoke(container.getCRUDService(),                                              new Object[]{bean.getValues()});        bean.setValues(valueObject);        if (LOG.isDebugEnabled()) {            LOG.debug("executeAction: State of the bean after performing"                      + " the action " + bean);        }        String forwardName = RequestProcessor.FORWARD_SUCCESS;        return findForward(mapping, request, valueObject, forwardName);    }    /**     * Get the service method to be invoked to service a CRUD request.  This     * will return either a create, read, update, or delete method for use.     * If the method name requested is 'save', either a create or     * update method will be returned based on the state of the value bean.     *     * @param methodName The the method to return, or 'save'     *                   to signal that the method must be determined based on     *                   the value bean's state     * @param bean The value bean used to determine whether a create or     *             update method should be returned     * @param request The HTTP request being processed.  If the request contains     *                a parameter called 'delete', a method for performing a     *                delete action will be returned     * @return The method to be used in servicing a CRUD request     */    private Method getMethod(String methodName, ValueBean bean,                                                HttpServletRequest request) {        if (LOG.isDebugEnabled()) {            LOG.debug("getMethod: The method to be performed is " + methodName);        }        if (request.getParameter(DELETE_PARAMETER) != null) {            // If the 'delete' request param is set, this is a delete action            methodName = DELETE_PARAMETER;        } else if (methodName.indexOf(ACTION_SAVE) != -1) {            int delimIndex = methodName.indexOf(DELIMETER);            int length = methodName.length();            String propertyName = methodName.substring(delimIndex + 1, length);            DTO values = bean.getValues();            if (LOG.isDebugEnabled()) {                LOG.debug("getMethod: The property to be checked is "                          + propertyName);                LOG.debug("getMethod: The values of the form bean are "                          + values);            }            String value = values.getValue(propertyName);            if (LOG.isDebugEnabled()) {                LOG.debug("getMethod: The identifier value is " + value);            }            if (Utilities.isBlank(value)                         || value.equals(ConstantValues.STRING_UNSAVED_VALUE)) {                methodName = ACTION_CREATE;            } else {                methodName = ACTION_UPDATE;            }        }        if (LOG.isDebugEnabled()) {            LOG.debug("getMethod: The method to be performed is " + methodName);        }        return (Method) METHOD_CACHE.get(methodName);    }}

⌨️ 快捷键说明

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