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

📄 requestutils.java

📁 java编程的一些小例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            return null;
        String name = mapping.getName();
        config = moduleConfig.findFormBeanConfig(name);
        if(config == null)
            return null;
        if(log.isDebugEnabled())
            log.debug(" Looking for ActionForm bean instance in scope '" + mapping.getScope() + "' under attribute key '" + attribute + "'");
        instance = null;
        HttpSession session = null;
        if("request".equals(mapping.getScope()))
        {
            instance = (ActionForm)request.getAttribute(attribute);
        }
        else
        {
            session = request.getSession();
            instance = (ActionForm)session.getAttribute(attribute);
        }
        if(instance == null)
            break MISSING_BLOCK_LABEL_404;
        if(config.getDynamic())
        {
            String className = ((DynaBean)instance).getDynaClass().getName();
            if(className.equals(config.getName()))
            {
                if(log.isDebugEnabled())
                {
                    log.debug(" Recycling existing DynaActionForm instance of type '" + className + "'");
                    log.trace(" --> " + instance);
                }
                return instance;
            }
            break MISSING_BLOCK_LABEL_404;
        }
        Class configClass = applicationClass(config.getType());
        if(!configClass.isAssignableFrom(instance.getClass()))
            break MISSING_BLOCK_LABEL_404;
        if(log.isDebugEnabled())
        {
            log.debug(" Recycling existing ActionForm instance of class '" + instance.getClass().getName() + "'");
            log.trace(" --> " + instance);
        }
        return instance;
        Throwable t;
        t;
        log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
        return null;
        if(config.getDynamic())
            try
            {
                DynaActionFormClass dynaClass = DynaActionFormClass.createDynaActionFormClass(config);
                instance = (ActionForm)dynaClass.newInstance();
                ((DynaActionForm)instance).initialize(mapping);
                if(log.isDebugEnabled())
                {
                    log.debug(" Creating new DynaActionForm instance of type '" + config.getType() + "'");
                    log.trace(" --> " + instance);
                }
            }
            // Misplaced declaration of exception variable
            catch(DynaActionFormClass dynaClass)
            {
                log.error(servlet.getInternal().getMessage("formBean", config.getType()), dynaClass);
                return null;
            }
        else
            try
            {
                instance = (ActionForm)applicationInstance(config.getType());
                if(log.isDebugEnabled())
                {
                    log.debug(" Creating new ActionForm instance of type '" + config.getType() + "'");
                    log.trace(" --> " + instance);
                }
            }
            // Misplaced declaration of exception variable
            catch(DynaActionFormClass dynaClass)
            {
                log.error(servlet.getInternal().getMessage("formBean", config.getType()), dynaClass);
                return null;
            }
        instance.setServlet(servlet);
        return instance;
    }

    public static Object lookup(PageContext pageContext, String name, String scopeName)
        throws JspException
    {
        if(scopeName == null)
            return pageContext.findAttribute(name);
        return pageContext.getAttribute(name, getScope(scopeName));
        JspException e;
        e;
        saveException(pageContext, e);
        throw e;
    }

    public static int getScope(String scopeName)
        throws JspException
    {
        Integer scope = (Integer)scopes.get(scopeName.toLowerCase());
        if(scope == null)
            throw new JspException(messages.getMessage("lookup.scope", scope));
        else
            return scope.intValue();
    }

    public static Object lookup(PageContext pageContext, String name, String property, String scope)
        throws JspException
    {
        Object bean;
        bean = lookup(pageContext, name, scope);
        if(bean == null)
        {
            JspException e = null;
            if(scope == null)
                e = new JspException(messages.getMessage("lookup.bean.any", name));
            else
                e = new JspException(messages.getMessage("lookup.bean", name, scope));
            saveException(pageContext, e);
            throw e;
        }
        if(property == null)
            return bean;
        return PropertyUtils.getProperty(bean, property);
        IllegalAccessException e;
        e;
        saveException(pageContext, e);
        throw new JspException(messages.getMessage("lookup.access", property, name));
        InvocationTargetException e;
        e;
        Throwable t = e.getTargetException();
        if(t == null)
            t = e;
        saveException(pageContext, t);
        throw new JspException(messages.getMessage("lookup.target", property, name));
        NoSuchMethodException e;
        e;
        saveException(pageContext, e);
        throw new JspException(messages.getMessage("lookup.method", property, name));
    }

    public static Locale retrieveUserLocale(PageContext pageContext, String locale)
    {
        Locale userLocale = null;
        HttpSession session = pageContext.getSession();
        if(locale == null)
            locale = "org.apache.struts.action.LOCALE";
        if(session != null)
            userLocale = (Locale)session.getAttribute(locale);
        if(userLocale == null)
            userLocale = pageContext.getRequest().getLocale();
        return userLocale;
    }

    public static String message(PageContext pageContext, String bundle, String locale, String key)
        throws JspException
    {
        return message(pageContext, bundle, locale, key, null);
    }

    public static String message(PageContext pageContext, String bundle, String locale, String key, Object args[])
        throws JspException
    {
        MessageResources resources = retrieveMessageResources(pageContext, bundle, false);
        Locale userLocale = retrieveUserLocale(pageContext, locale);
        if(args == null)
            return resources.getMessage(userLocale, key);
        else
            return resources.getMessage(userLocale, key, args);
    }

    private static MessageResources retrieveMessageResources(PageContext pageContext, String bundle, boolean checkPageScope)
        throws JspException
    {
        MessageResources resources = null;
        if(bundle == null)
            bundle = "org.apache.struts.action.MESSAGE";
        if(checkPageScope)
            resources = (MessageResources)pageContext.getAttribute(bundle, 1);
        if(resources == null)
            resources = (MessageResources)pageContext.getAttribute(bundle, 2);
        if(resources == null)
            resources = (MessageResources)pageContext.getAttribute(bundle, 4);
        if(resources == null)
        {
            JspException e = new JspException(messages.getMessage("message.bundle", bundle));
            saveException(pageContext, e);
            throw e;
        }
        else
        {
            return resources;
        }
    }

    public static void populate(Object bean, HttpServletRequest request)
        throws ServletException
    {
        populate(bean, null, null, request);
    }

    public static void populate(Object bean, String prefix, String suffix, HttpServletRequest request)
        throws ServletException
    {
        HashMap properties = new HashMap();
        Enumeration names = null;
        Map multipartParameters = null;
        String contentType = request.getContentType();
        String method = request.getMethod();
        boolean isMultipart = false;
        if(contentType != null && contentType.startsWith("multipart/form-data") && method.equalsIgnoreCase("POST"))
        {
            ActionServletWrapper servlet;
            if(bean instanceof ActionForm)
                servlet = ((ActionForm)bean).getServletWrapper();
            else
                throw new ServletException("bean that's supposed to be populated from a multipart request is not of type \"org.apache.struts.action.ActionForm\", but type \"" + bean.getClass().getName() + "\"");
            MultipartRequestHandler multipartHandler = getMultipartHandler(request);
            ((ActionForm)bean).setMultipartRequestHandler(multipartHandler);
            if(multipartHandler != null)
            {
                isMultipart = true;
                servlet.setServletFor(multipartHandler);
                multipartHandler.setMapping((ActionMapping)request.getAttribute("org.apache.struts.action.mapping.instance"));
                multipartHandler.handleRequest(request);
                Boolean maxLengthExceeded = (Boolean)request.getAttribute("org.apache.struts.upload.MaxLengthExceeded");
                if(maxLengthExceeded != null && maxLengthExceeded.booleanValue())
                    return;
                multipartParameters = getAllParametersForMultipartRequest(request, multipartHandler);
                names = Collections.enumeration(multipartParameters.keySet());
            }
        }
        if(!isMultipart)
            names = request.getParameterNames();
        while(names.hasMoreElements()) 
        {
            String name = (String)names.nextElement();
            String stripped = name;
            if(prefix != null)
            {
                if(!stripped.startsWith(prefix))
                    continue;
                stripped = stripped.substring(prefix.length());
            }
            if(suffix != null)
            {
                if(!stripped.endsWith(suffix))
                    continue;
                stripped = stripped.substring(0, stripped.length() - suffix.length());
            }
            if(isMultipart)
                properties.put(stripped, multipartParameters.get(name));
            else
                properties.put(stripped, request.getParameterValues(name));
        }

        try
        {
            BeanUtils.populate(bean, properties);
        }
        catch(Exception e)
        {
            throw new ServletException("BeanUtils.populate", e);
        }
    }

    private static MultipartRequestHandler getMultipartHandler(HttpServletRequest request)
        throws ServletException
    {
        MultipartRequestHandler multipartHandler = null;
        String multipartClass = (String)request.getAttribute("org.apache.struts.action.mapping.multipartclass");
        request.removeAttribute("org.apache.struts.action.mapping.multipartclass");
        if(multipartClass != null)
        {
            try
            {
                multipartHandler = (MultipartRequestHandler)applicationInstance(multipartClass);
            }
            catch(ClassNotFoundException cnfe)
            {
                log.error("MultipartRequestHandler class \"" + multipartClass + "\" in mapping class not found, " + "defaulting to global multipart class");
            }
            catch(InstantiationException ie)
            {
                log.error("InstantiaionException when instantiating MultipartRequestHandler \"" + multipartClass + "\", " + "defaulting to global multipart class, exception: " + ie.getMessage());
            }
            catch(IllegalAccessException iae)
            {
                log.error("IllegalAccessException when instantiating MultipartRequestHandler \"" + multipartClass + "\", " + "defaulting to global multipart class, exception: " + iae.getMessage());
            }
            if(multipartHandler != null)
                return multipartHandler;
        }
        ModuleConfig moduleConfig = (ModuleConfig)request.getAttribute("org.apache.struts.action.MODULE");
        multipartClass = moduleConfig.getControllerConfig().getMultipartClass();
        if(multipartClass != null)
        {
            try
            {
                multipartHandler = (MultipartRequestHandler)applicationInstance(multipartClass);
            }
            catch(ClassNotFoundException cnfe)
            {
                throw new ServletException("Cannot find multipart class \"" + multipartClass + "\"" + ", exception: " + cnfe.getMessage());
            }
            catch(InstantiationException ie)
            {
                throw new ServletException("InstantiaionException when instantiating multipart class \"" + multipartClass + "\", exception: " + ie.getMessage());
            }
            catch(IllegalAccessException iae)
            {
                throw new ServletException("IllegalAccessException when instantiating multipart class \"" + multipartClass + "\", exception: " + iae.getMessage());
            }
            if(multipartHandler != null)
                return multipartHandler;
        }
        return multipartHandler;
    }

    private static Map getAllParametersForMultipartRequest(HttpServletRequest request, MultipartRequestHandler multipartHandler)
    {
        Map parameters = new HashMap();
        Hashtable elements = multipartHandler.getAllElements();
        String key;
        for(Enumeration enum = elements.keys(); enum.hasMoreElements(); parameters.put(key, elements.get(key)))
            key = (String)enum.nextElement();

        if(request instanceof MultipartRequestWrapper)
        {
            request = ((MultipartRequestWrapper)request).getRequest();
            String key;
            for(Enumeration enum = request.getParameterNames(); enum.hasMoreElements(); parameters.put(key, request.getParameterValues(key)))
                key = (String)enum.nextElement();

        }
        else
        {
            log.debug("Gathering multipart parameters for unwrapped request");
        }
        return parameters;
    }

    public static boolean present(PageContext pageContext, String bundle, String locale, String key)
        throws JspException
    {
        MessageResources resources = retrieveMessageResources(pageContext, bundle, true);
        Locale userLocale = retrieveUserLocale(pageContext, locale);
        return resources.isPresent(userLocale, key);
    }

    public static String printableURL(URL url)
    {
        if(url.getHost() != null)
            return url.toString();
        String file = url.getFile();
        String ref = url.getRef();
        if(ref == null)
        {
            return file;
        }
        else
        {
            StringBuffer sb = new StringBuffer(file);
            sb.append('#');
            sb.append(ref);
            return sb.toString();
        }
    }

    public static String actionURL(HttpServletRequest request, ActionConfig action, String pattern)
    {
        StringBuffer sb = new StringBuffer();
        if(pattern.endsWith("/*"))
        {
            sb.append(pattern.substring(0, pattern.length() - 2));
            sb.append(action.getPath());
        }
        else
        if(pattern.startsWith("*."))
        {
            ModuleConfig appConfig = (ModuleConfig)request.getAttribute("org.apache.struts.action.MODULE");
            sb.append(appConfig.getPrefix());
            sb.append(action.getPath());
            sb.append(pattern.substring(1));
        }
        else
        {
            throw new IllegalArgumentException(pattern);
        }
        return sb.toString();
    }

    public static String forwardURL(HttpServletRequest request, ForwardConfig forward)
    {
        String path = forward.getPath();

⌨️ 快捷键说明

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