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

📄 tagutils.java

📁 structs源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @return current user locale
     */
    public Locale getUserLocale(PageContext pageContext, String locale) {
        return RequestUtils.getUserLocale((HttpServletRequest) pageContext
            .getRequest(), locale);
    }

    /**
     * Returns true if the custom tags are in XHTML mode.
     */
    public boolean isXhtml(PageContext pageContext) {
        String xhtml =
            (String) pageContext.getAttribute(Globals.XHTML_KEY,
                PageContext.PAGE_SCOPE);

        return "true".equalsIgnoreCase(xhtml);
    }

    /**
     * Locate and return the specified bean, from an optionally specified
     * scope, in the specified page context.  If no such bean is found, return
     * <code>null</code> instead.  If an exception is thrown, it will have
     * already been saved via a call to <code>saveException()</code>.
     *
     * @param pageContext Page context to be searched
     * @param name        Name of the bean to be retrieved
     * @param scopeName   Scope to be searched (page, request, session,
     *                    application) or <code>null</code> to use
     *                    <code>findAttribute()</code> instead
     * @return JavaBean in the specified page context
     * @throws JspException if an invalid scope name is requested
     */
    public Object lookup(PageContext pageContext, String name, String scopeName)
        throws JspException {
        if (scopeName == null) {
            return pageContext.findAttribute(name);
        }

        try {
            return pageContext.getAttribute(name, instance.getScope(scopeName));
        } catch (JspException e) {
            saveException(pageContext, e);
            throw e;
        }
    }

    /**
     * Locate and return the specified property of the specified bean, from an
     * optionally specified scope, in the specified page context.  If an
     * exception is thrown, it will have already been saved via a call to
     * <code>saveException()</code>.
     *
     * @param pageContext Page context to be searched
     * @param name        Name of the bean to be retrieved
     * @param property    Name of the property to be retrieved, or
     *                    <code>null</code> to retrieve the bean itself
     * @param scope       Scope to be searched (page, request, session,
     *                    application) or <code>null</code> to use
     *                    <code>findAttribute()</code> instead
     * @return property of specified JavaBean
     * @throws JspException if an invalid scope name is requested
     * @throws JspException if the specified bean is not found
     * @throws JspException if accessing this property causes an
     *                      IllegalAccessException, IllegalArgumentException,
     *                      InvocationTargetException, or NoSuchMethodException
     */
    public Object lookup(PageContext pageContext, String name, String property,
        String scope) throws JspException {
        // Look up the requested bean, and return if requested
        Object 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;
        }

        // Locate and return the specified property
        try {
            return PropertyUtils.getProperty(bean, property);
        } catch (IllegalAccessException e) {
            saveException(pageContext, e);
            throw new JspException(messages.getMessage("lookup.access",
                    property, name));
        } catch (IllegalArgumentException e) {
            saveException(pageContext, e);
            throw new JspException(messages.getMessage("lookup.argument",
                    property, name));
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();

            if (t == null) {
                t = e;
            }

            saveException(pageContext, t);
            throw new JspException(messages.getMessage("lookup.target",
                    property, name));
        } catch (NoSuchMethodException e) {
            saveException(pageContext, e);

            String beanName = name;

            // Name defaults to Contants.BEAN_KEY if no name is specified by
            // an input tag. Thus lookup the bean under the key and use
            // its class name for the exception message.
            if (Constants.BEAN_KEY.equals(name)) {
                Object obj = pageContext.findAttribute(Constants.BEAN_KEY);

                if (obj != null) {
                    beanName = obj.getClass().getName();
                }
            }

            throw new JspException(messages.getMessage("lookup.method",
                    property, beanName));
        }
    }

    /**
     * Look up and return a message string, based on the specified
     * parameters.
     *
     * @param pageContext The PageContext associated with this request
     * @param bundle      Name of the servlet context attribute for our
     *                    message resources bundle
     * @param locale      Name of the session attribute for our user's Locale
     * @param key         Message key to be looked up and returned
     * @return message string
     * @throws JspException if a lookup error occurs (will have been saved in
     *                      the request already)
     */
    public String message(PageContext pageContext, String bundle,
        String locale, String key)
        throws JspException {
        return message(pageContext, bundle, locale, key, null);
    }

    /**
     * Look up and return a message string, based on the specified
     * parameters.
     *
     * @param pageContext The PageContext associated with this request
     * @param bundle      Name of the servlet context attribute for our
     *                    message resources bundle
     * @param locale      Name of the session attribute for our user's Locale
     * @param key         Message key to be looked up and returned
     * @param args        Replacement parameters for this message
     * @return message string
     * @throws JspException if a lookup error occurs (will have been saved in
     *                      the request already)
     */
    public String message(PageContext pageContext, String bundle,
        String locale, String key, Object[] args)
        throws JspException {
        MessageResources resources =
            retrieveMessageResources(pageContext, bundle, false);

        Locale userLocale = getUserLocale(pageContext, locale);
        String message = null;

        if (args == null) {
            message = resources.getMessage(userLocale, key);
        } else {
            message = resources.getMessage(userLocale, key, args);
        }

        if ((message == null) && log.isDebugEnabled()) {
            // log missing key to ease debugging
            log.debug(resources.getMessage("message.resources", key, bundle,
                    locale));
        }

        return message;
    }

    /**
     * <p>Return the context-relative URL that corresponds to the specified
     * <code>page</code> attribute value, calculated based on the
     * <code>pagePattern</code> property of the current module's {@link
     * ModuleConfig}.</p>
     *
     * @param request The servlet request we are processing
     * @param page    The module-relative URL to be substituted in to the
     *                <code>pagePattern</code> pattern for the current module
     *                (<strong>MUST</strong> start with a slash)
     * @return context-relative URL
     */
    public String pageURL(HttpServletRequest request, String page,
        ModuleConfig moduleConfig) {
        StringBuffer sb = new StringBuffer();
        String pagePattern =
            moduleConfig.getControllerConfig().getPagePattern();

        if (pagePattern == null) {
            sb.append(moduleConfig.getPrefix());
            sb.append(page);
        } else {
            boolean dollar = false;

            for (int i = 0; i < pagePattern.length(); i++) {
                char ch = pagePattern.charAt(i);

                if (dollar) {
                    switch (ch) {
                    case 'M':
                        sb.append(moduleConfig.getPrefix());

                        break;

                    case 'P':
                        sb.append(page);

                        break;

                    case '$':
                        sb.append('$');

                        break;

                    default:
                        ; // Silently swallow
                    }

                    dollar = false;

                    continue;
                } else if (ch == '$') {
                    dollar = true;
                } else {
                    sb.append(ch);
                }
            }
        }

        return sb.toString();
    }

    /**
     * Return true if a message string for the specified message key is
     * present for the specified <code>Locale</code> and bundle.
     *
     * @param pageContext The PageContext associated with this request
     * @param bundle      Name of the servlet context attribute for our
     *                    message resources bundle
     * @param locale      Name of the session attribute for our user's Locale
     * @param key         Message key to be looked up and returned
     * @return true if a message string for message key exists
     * @throws JspException if a lookup error occurs (will have been saved in
     *                      the request already)
     */
    public boolean present(PageContext pageContext, String bundle,
        String locale, String key)
        throws JspException {
        MessageResources resources =
            retrieveMessageResources(pageContext, bundle, true);

        Locale userLocale = getUserLocale(pageContext, locale);

        return resources.isPresent(userLocale, key);
    }

    /**
     * Returns the appropriate MessageResources object for the current module
     * and the given bundle.
     *
     * @param pageContext    Search the context's scopes for the resources.
     * @param bundle         The bundle name to look for.  If this is
     *                       <code>null</code>, the default bundle name is
     *                       used.
     * @param checkPageScope Whether to check page scope
     * @return MessageResources The bundle's resources stored in some scope.
     * @throws JspException if the MessageResources object could not be
     *                      found.
     */
    public MessageResources retrieveMessageResources(PageContext pageContext,
        String bundle, boolean checkPageScope)
        throws JspException {
        MessageResources resources = null;

        if (bundle == null) {
            bundle = Globals.MESSAGES_KEY;
        }

        if (checkPageScope) {
            resources =
                (MessageResources) pageContext.getAttribute(bundle,
                    PageContext.PAGE_SCOPE);
        }

        if (resources == null) {
            resources =
                (MessageResources) pageContext.getAttribute(bundle,
                    PageContext.REQUEST_SCOPE);
        }

        if (resources == null) {
            ModuleConfig moduleConfig = getModuleConfig(pageContext);

            resources =
                (MessageResources) pageContext.getAttribute(bundle
                    + moduleConfig.getPrefix(), PageContext.APPLICATION_SCOPE);
        }

        if (resources == null) {
            resources =
                (MessageResources) pageContext.getAttribute(bundle,
                    PageContext.APPLICATION_SCOPE);
        }

        if (resources == null) {
            JspException e =
                new JspException(messages.getMessage("message.bundle", bundle));

            saveException(pageContext, e);
            throw e;
        }

        return resources;
    }

    /**
     * Save the specified exception as a request attribute for later use.
     *
     * @param pageContext The PageContext for the current page
     * @param exception   The exception to be saved
     */
    public void saveException(PageContext pageContext, Throwable exception) {
        pageContext.setAttribute(Globals.EXCEPTION_KEY, exception,
            PageContext.REQUEST_SCOPE);
    }

    /**
     * Write the specified text as the response to the writer associated with
     * this page.  <strong>WARNING</strong> - If you are writing body content
     * from the <code>doAfterBody()</code> method of a custom tag class that
     * implements <code>BodyTag</code>, you should be calling
     * <code>writePrevious()</code> instead.
     *
     * @param pageContext The PageContext object for this page
     * @param text        The text to be written
     * @throws JspException if an input/output error occurs (already saved)
     */
    public void write(PageContext pageContext, String text)
        throws JspException {
        JspWriter writer = pageContext.getOut();

        try {
            writer.print(text);
        } catch (IOException e) {
            saveException(pageContext, e);
            throw new JspException(messages.getMessage("write.io", e.toString()));
        }
    }

    /**
     * Write the specified text as the response to the writer associated with
     * the body content for the tag within which we are currently nested.
     *
     * @param pageContext The PageContext object for this page
     * @param text        The text to be written
     * @throws JspException if an input/output error occurs (already saved)
     */
    public void writePrevious(PageContext pageContext, String text)
        throws JspException {
        JspWriter writer = pageContext.getOut();

        if (writer instanceof BodyContent) {
            writer = ((BodyContent) writer).getEnclosingWriter();
        }

        try {
            writer.print(text);
        } catch (IOException e) {
            saveException(pageContext, e);
            throw new JspException(messages.getMessage("write.io", e.toString()));
        }
    }
}

⌨️ 快捷键说明

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