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

📄 baseformcontroller.java

📁 一个很好的开源项目管理系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    protected void saveToken(HttpServletRequest request) {        request.getSession().setAttribute(TOKEN_ATTRIBUTE_NAME,                new RandomGuid().toString());    }    /**     * Remove the transaction token in the users session     * @param request The request     */    protected void clearToken(HttpServletRequest request) {        request.getSession().removeAttribute(TOKEN_ATTRIBUTE_NAME);    }    /**     * Check the transaction token available in the request matches the current     * transaction token in users session. Used to prevent multiple submits.     * @param request The http request     * @return whether the token is valid     */    protected boolean isTokenValid(HttpServletRequest request) {        String token = request.getParameter(TOKEN_PARAMETER_NAME);        if (token != null                && token.equals(request.getSession().getAttribute(                        TOKEN_ATTRIBUTE_NAME))) {            return true;        }        else {            logger.info("Invalid token. token in request=" + token                    + ". token in session="                    + request.getSession().getAttribute(TOKEN_ATTRIBUTE_NAME));            return false;        }    }    /**     * The forms attribute name. The spring configured 'beanName' is part of the     * name. prefix the attribute name with 'FORM." so that the can be     * identified to be cleaned up by one of the interceptors.     */    protected String getFormSessionAttributeName() {        return isSessionForm() ? "FORM." + beanName + "." + getCommandName()                : null;    }    /**     * Creates a ViewHelper and stores it in users session. The ViewHelper is     * used by the jsps to render the form. If the 'mode' in RequestContext is     * set by downstream processing, that mode value is used to create the     * ViewHelper, otherwise, the 'defaultMode' is used.     * @param request The http request     * @param defaultMode The default mode     */    protected void saveViewHelper(HttpServletRequest request, String defaultMode) {        String mode = RequestContextHolder.getRequestContext().getMode();        if (mode == null) {            if (logger.isInfoEnabled())                logger.info("creating ViewHelper with default mode: "                        + defaultMode);            request.getSession().setAttribute(WebConstants.VIEW_HELPER,                    new ViewHelper(defaultMode));        }        else {            if (logger.isInfoEnabled())                logger                        .info("creating ViewHelper using  RequestContextHolder. mode: "                                + mode);            request.getSession().setAttribute(WebConstants.VIEW_HELPER,                    new ViewHelper(mode));        }    }    /**     * Save messages to the messages queue so that they are available to the     * jsps     * @param request the http request     * @param msgCode the message code     */    public void saveMessage(HttpServletRequest request, String msgCode) {        saveMessage(request, msgCode, null);    }    /**     * Save messages to the messages queue so that they are available to the     * jsps     * @param request the http request     * @param msgCode the message code     * @param msgArgs the message arguments     */    public void saveMessage(HttpServletRequest request, String msgCode,            String msgArgs) {        List messages = (List) request.getAttribute(MESSAGES_ATTRIBUTE_NAME);        if (messages == null)            messages = new ArrayList();        messages.add(new CodeAndArguments(msgCode, msgArgs));        request.setAttribute(MESSAGES_ATTRIBUTE_NAME, messages);    }    /**     * Populate 'errors' with the information in the PropertyException.     * @param errors The errors object.     * @param pe The propertyException     */    protected void populatePropertyErrors(Errors errors, IPropertyException pe) {        String property = null;        if (pe.isMultiProperty()) {            List list = pe.getPropertyNameList();            for (int i = 0; i < list.size(); i++) {                property = (String) list.get(i);                if (i == 0) {                    if (property != null)                        errors.rejectValue(property, pe.getErrorCode());                    else                        errors.reject(pe.getErrorCode());                }                else {                    // We just want blank messages for all properties except                    // the first.                    if (property != null)                        errors.rejectValue(property, "errors.blank");                }            }        }        else {            if (pe.getPropertyName() != null)                errors.rejectValue(pe.getPropertyName(), pe.getErrorCode());            else                errors.reject(pe.getErrorCode());        }    }    /**     * show form when an exception occurs. When an exception occurs in the     * business layer the domain object (command) could be in an inconsistent     * state (Ex. the version could have been incremented but the save to the     * database failed for some reason). Since we want to redisplay the form to     * the user in the case of recoverable exceptions, repopulate the command     * object with the data in the http request and redisplay the form . Now the     * form will reflect the same information the user submitted.     * @param request the request     * @param command the command     * @param errors the bind exception     * @return the Model and view     */    protected ModelAndView showFormWhenException(HttpServletRequest request,            HttpServletResponse response, Object command, BindException errors) {        if (logger.isInfoEnabled())            logger.info("showFormWhenException() invoked.");        try {            // repopluate the command object with the request paramaters            ServletRequestDataBinder dataBinder = createBinder(request, command);            dataBinder.bind(request);            return showForm(request, response, errors);        }        catch (Exception e) {            throw new RuntimeException(e);        }    }    /**     * The attribute name when using page flow to set a value on a form.     * @return the attribute name     */    protected String getFlowSetValueHelperAttributeName() {        // suffix with 'FormHelper' so it gets cleaned up by SessionCleanupInterceptor.        return beanName + "." + getCommandName() + ".flowFormHelper";    }    /**     * The controller path configured in the *-servlet.xml for this request     * without the leading '/' (if any)     * @param request The http request     * @return the controller path     */    protected String getControllerPath(HttpServletRequest request) {        String controllerPath = new UrlPathHelper()                .getLookupPathForRequest(request);        if (logger.isInfoEnabled())            logger.info("controller path:" + controllerPath);        if (controllerPath.startsWith("/"))            return controllerPath.substring(1);        else            return controllerPath;    }    /**     * Get the users security profile. This is stored in session when user logs     * in.     * @param request The http request     * @return The users security profile     */    public ISecurityProfile getSecurityProfile(HttpServletRequest request) {        return (ISecurityProfile) request.getSession().getAttribute(                WebConstants.SECURITY_PROFILE);    }    /**     * Retrieve the service method name using reflection     * @param serviceClass The service class     * @param methodName The method name     * @param args The args for the method     * @return The service method     */    protected Method getServiceMethod(Class serviceClass, String methodName,            Class[] args) {        try {            return serviceClass.getMethod(methodName, args);        }        catch (NoSuchMethodException e) {            throw new RuntimeException("Could not find method " + methodName                    + " in service", e);        }    }    //TODO handle all types    /**     * Get the parameter value. Can handle types String and Long *only*     * @param request Request     * @param parameterName the parameter name     * @param parameterClass The type of the parameter     * @return The parameter value     */    protected Object getParameterValue(HttpServletRequest request,            String parameterName, Class parameterClass) {        String param = request.getParameter(parameterName);        if (param != null && param.length() > 0) {            if (parameterClass == String.class)                return param;            else                return new Long(param);        }        else            return null;    }    protected SimpleDateFormat getDateFormatter(HttpServletRequest request) {        String dateFormat = getWebApplicationContext().getMessage("dateFormat",                null, RequestContextUtils.getLocale(request));        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);        formatter.setLenient(false);        return formatter;    }}

⌨️ 快捷键说明

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