Example usage for javax.servlet.jsp PageContext findAttribute

List of usage examples for javax.servlet.jsp PageContext findAttribute

Introduction

In this page you can find the example usage for javax.servlet.jsp PageContext findAttribute.

Prototype


abstract public Object findAttribute(String name);

Source Link

Document

Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

Usage

From source file:org.rhq.enterprise.gui.legacy.taglib.display.TableTag.java

/**
 * This functionality is borrowed from struts, but I've removed some struts specific features so that this tag can
 * be used both in a struts application, and outside of one.
 *
 * <p/>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.
 *
 * @param  pageContext Page context to be searched
 * @param  name        Name of the bean to be retrieved
 * @param  scope       Scope to be searched (page, request, session, application) or <code>null</code> to use <code>
 *                     findAttribute()</code> instead
 *
 * @throws JspException if an invalid scope name is requested
 *//*  w  w  w . j  a  va2  s .c  om*/

public Object lookup(PageContext pageContext, String name, String scope) throws JspException {
    log.trace("looking up: " + name + " in scope: " + scope);

    Object bean;
    if (scope == null) {
        bean = pageContext.findAttribute(name);
    } else if (scope.equalsIgnoreCase("page")) {
        bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
    } else if (scope.equalsIgnoreCase("request")) {
        bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
    } else if (scope.equalsIgnoreCase("session")) {
        bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
    } else if (scope.equalsIgnoreCase("application")) {
        bean = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
    } else {
        Object[] objs = { name, scope };
        if (prop.getProperty("error.msg.cant_find_bean") != null) {
            String msg = MessageFormat.format(prop.getProperty("error.msg.cant_find_bean"), objs);
            throw new JspException(msg);
        } else {
            throw new JspException("Could not find " + name + " in scope " + scope);
        }
    }

    return (bean);
}

From source file:org.squale.welcom.taglib.field.util.LayoutUtils.java

/**
 * Get the property 'property' of the bean named WConstants.BEAN_KEY in the given page context Handle classic
 * exception//from   ww w  .jav  a  2  s . c  o  m
 * 
 * @param pageContext le page context
 * @param property le nom de la property
 * @throws JspException exception pouvant etre levee
 * @return le bean
 */
public static Object getBeanFromPageContext(final PageContext pageContext, final String property)
        throws JspException {
    final Object bean = pageContext.findAttribute(name);

    if (bean == null) {
        throw new JspException(messages.getMessage("getter.bean", name));
    }

    Object object = bean;

    if (property != null) {
        try {
            object = PropertyUtils.getProperty(bean, property);
        } catch (final IllegalAccessException e) {
            throw new JspException(messages.getMessage("getter.access", property, name));
        } catch (final InvocationTargetException e) {
            final Throwable t = e.getTargetException();
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        } catch (final NoSuchMethodException e) {
            throw new JspException(messages.getMessage("getter.method", property, name));
        }
    }

    return object;
}

From source file:org.squale.welcom.taglib.field.util.LayoutUtils.java

/**
 * Get the property 'property' of the bean named 'name' in the given pageContext. Handle classic Exception
 * //  w ww.java 2s  .c o m
 * @param pageContext le page context
 * @param pName le nom du bean
 * @param property le nom de la property
 * @throws JspException exception pouvant etre levee
 * @return le bean
 */
public static Object getBeanFromPageContext(final PageContext pageContext, final String pName,
        final String property) throws JspException {
    final Object bean = pageContext.findAttribute(pName);

    if (bean == null) {
        throw new JspException(messages.getMessage("getter.bean", pName));
    }

    Object object = bean;

    if (property != null) {
        try {
            object = PropertyUtils.getProperty(bean, property);
        } catch (final IllegalAccessException e) {
            throw new JspException(messages.getMessage("getter.access", property, pName));
        } catch (final InvocationTargetException e) {
            final Throwable t = e.getTargetException();
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        } catch (final NoSuchMethodException e) {
            throw new JspException(messages.getMessage("getter.method", property, pName));
        }
    }

    return object;
}

From source file:org.squale.welcom.taglib.field.util.LayoutUtils.java

/**
 * Get the label with the key 'key' from the pageContext messageRessources. Handle classic exception
 * //from   w w  w. j  a v a 2  s .  c  om
 * @param pageContext le page context
 * @param pBundle le bundle
 * @param key la cle
 * @param args les arguments
 * @throws JspException exception pouvant etre levee
 * @return le label
 */
public static String getLabel(final PageContext pageContext, final String pBundle, final String key,
        final Object args[]) throws JspException {
    // Acquire the resources object containing our messages
    final MessageResources resources = (MessageResources) pageContext.findAttribute(pBundle);

    if (resources == null) {
        throw new JspException(messages.getMessage("messageTag.resources", pBundle));
    }

    // Calculate the Locale we will be using
    Locale locale = null;

    try {
        locale = (Locale) pageContext.getAttribute(localeKey, PageContext.SESSION_SCOPE);
    } catch (final IllegalStateException e) { // Invalidated session
        locale = null;
    }

    if (locale == null) {
        locale = Locale.getDefault();
    }

    // Retrieve the message string we are looking for
    final String message = resources.getMessage(locale, key, args);

    if (message == null) {
        return key;
    }

    return message;
}