Java Code Examples for javax.servlet.jsp.PageContext#findAttribute()

The following examples show how to use javax.servlet.jsp.PageContext#findAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScopedAttributeELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(ELContext context, Object base, Object property)
        throws NullPointerException, PropertyNotFoundException, ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        if (property != null) {
            String key = property.toString();
            PageContext page = (PageContext) context
                    .getContext(JspContext.class);
            return page.findAttribute(key);
        }
    }

    return null;
}
 
Example 2
Source File: ScopedAttributeELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(ELContext context, Object base, Object property)
        throws NullPointerException, PropertyNotFoundException, ELException {
    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        if (property != null) {
            String key = property.toString();
            PageContext page = (PageContext) context
                    .getContext(JspContext.class);
            return page.findAttribute(key);
        }
    }

    return null;
}
 
Example 3
Source File: AttributeMap.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public Object get(Object key) {
    PageContext pc = getPageContext();
    if (pc == null) {
        Map request = (Map) context.get("request");
        Map session = (Map) context.get("session");
        Map application = (Map) context.get("application");
        if ((request != null) && (request.get(key) != null)) {
            return request.get(key);
        } else if ((session != null) && (session.get(key) != null)) {
            return session.get(key);
        } else if ((application != null) && (application.get(key) != null)) {
            return application.get(key);
        }
    } else {
        try {
            return pc.findAttribute(key.toString());
        } catch (NullPointerException npe) {
            return null;
        }
    }
    return null;
}
 
Example 4
Source File: InteractionELSupport.java    From ontopia with Apache License 2.0 6 votes vote down vote up
/**
 * Breaks name up into tokens separated by '.' characters,
 * looks up the first token (in other JSTL scopes) to get a variable.
 * Uses the next token (if there) to get a bean property from the variable.
 * Uses additional tokens to lookup bean properties of bean properties.
 * If the variable or any of those bean properties is a Map
 * then gets the object with the given token as key.
 * @param name The name to lookup.
 * @param pageContext Used to look up attributes.
 */
public static Object getValue(String name, PageContext pageContext) {
  log.debug("Looking up name: " + name + " in PageContext.");
  StringTokenizer tok = new StringTokenizer(name);
  if (tok.hasMoreTokens()) {
    String currentName = tok.nextToken(".");
    log.debug("firstName " + currentName);
    Object retVal = pageContext.findAttribute(currentName);
    
    while (tok.hasMoreTokens()) {
      currentName = tok.nextToken(".");
      log.debug("currentName " + currentName);
      retVal = getBeanProperty(retVal, currentName);
      log.debug("current object: " + retVal);
    }
    return retVal;
  }
  
  return null;
}
 
Example 5
Source File: BlockTag.java    From jsp-template-inheritance with Apache License 2.0 5 votes vote down vote up
private PutType getPutType(PageContext pageContext) {
    PutType putType = (PutType) pageContext.findAttribute(getBlockTypeAttributeName(name));
    if (putType == null) {
        return DEFAULT_PUT_TYPE;
    }
    return putType;
}
 
Example 6
Source File: BlockTag.java    From jsp-template-inheritance with Apache License 2.0 5 votes vote down vote up
private String getPutContents(PageContext pageContext) {
    String putContents = (String) pageContext.findAttribute(getBlockContentsAttributeName(name));
    if (putContents == null) {
        return "";
    }
    return putContents;
}
 
Example 7
Source File: ScopedAttributeELResolver.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public Object getValue(ELContext context, Object base, Object property) {
    Objects.requireNonNull(context);

    Object result = null;

    if (base == null) {
        context.setPropertyResolved(base, property);
        if (property != null) {
            String key = property.toString();
            PageContext page = (PageContext) context.getContext(JspContext.class);
            result = page.findAttribute(key);

            if (result == null) {
                boolean resolveClass = true;
                // Performance short-cut available when running on Tomcat
                if (AST_IDENTIFIER_KEY != null) {
                    // Tomcat will set this key to Boolean.TRUE if the
                    // identifier is a stand-alone identifier (i.e.
                    // identifier) rather than part of an AstValue (i.e.
                    // identifier.something). Imports do not need to be
                    // checked if this is a stand-alone identifier
                    Boolean value = (Boolean) context.getContext(AST_IDENTIFIER_KEY);
                    if (value != null && value.booleanValue()) {
                        resolveClass = false;
                    }
                }
                // This might be the name of an imported class
                ImportHandler importHandler = context.getImportHandler();
                if (importHandler != null) {
                    Class<?> clazz = null;
                    if (resolveClass) {
                        clazz = importHandler.resolveClass(key);
                    }
                    if (clazz != null) {
                        result = new ELClass(clazz);
                    }
                    if (result == null) {
                        // This might be the name of an imported static field
                        clazz = importHandler.resolveStatic(key);
                        if (clazz != null) {
                            try {
                                result = clazz.getField(key).get(null);
                            } catch (IllegalArgumentException | IllegalAccessException |
                                    NoSuchFieldException | SecurityException e) {
                                // Most (all?) of these should have been
                                // prevented by the checks when the import
                                // was defined.
                            }
                        }
                    }
                }
            }
        }
    }

    return result;
}
 
Example 8
Source File: ScopedAttributeELResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * If the base object is <code>null</code>, searches the page, 
 * request, session and application scopes for an attribute with
 * the given name and returns it, or <code>null</code> if no
 * attribute exists with the current name.
 *
 * <p>The <code>propertyResolved</code> property of the 
 * <code>ELContext</code> object must be set to <code>true</code> by 
 * this resolver before returning if base is <code>null</code>. If 
 * this property is not <code>true</code> after this method is called,
 * the caller should ignore the return value.</p>
 *
 * @param context The context of this evaluation.
 * @param base Only <code>null</code> is handled by this resolver.
 *     Other values will result in an immediate return.
 * @param property The name of the scoped attribute to resolve.
 * @return If the <code>propertyResolved</code> property of 
 *     <code>ELContext</code> was set to <code>true</code>, then
 *     the scoped attribute; otherwise undefined.
 * @throws NullPointerException if context is <code>null</code>
 * @throws ELException if an exception was thrown while performing
 *     the property or variable resolution. The thrown exception
 *     must be included as the cause property of this exception, if
 *     available.
 */
public Object getValue(ELContext context,
                       Object base,
                       Object property) {

    if (context == null) {
        throw new NullPointerException();
    }

    if (base == null) {
        context.setPropertyResolved(true);
        if (property instanceof String) {
            String attribute = (String) property;
            PageContext ctxt = (PageContext)
                                   context.getContext(JspContext.class);
            return ctxt.findAttribute(attribute);
        }
    }
    return null;
}
 
Example 9
Source File: TagUtils.java    From feilong-taglib with Apache License 2.0 4 votes vote down vote up
/**
 * Find attribute value.
 * 
 * @param <T>
 *
 * @param pageContext
 *            the page context
 * @param attributeName
 *            the version name in scope
 * @param scope
 *            the version search scope
 * @return 如果 <code>pageContext</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>attributeName</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>attributeName</code> 是blank,抛出 {@link IllegalArgumentException}<br>
 *         如果 <code>scope</code> 是null或者 blank,将会调用 {@link javax.servlet.jsp.JspContext#findAttribute(String)}<br>
 *         如果 <code>scope</code> 不是null或者 blank,将会调用 {@link javax.servlet.jsp.JspContext#getAttribute(String, int)}<br>
 * @since 1.12.8
 */
@SuppressWarnings("unchecked")
public static <T> T findAttributeValue(PageContext pageContext,String attributeName,String scope){
    Validate.notNull(pageContext, "pageContext can't be null!");
    Validate.notBlank(attributeName, "attributeName can't be blank!");

    //---------------------------------------------------------------

    //没有指定scope 那么从pageContext中查找
    if (isNullOrEmpty(scope)){
        return (T) pageContext.findAttribute(attributeName);
    }

    //如果指定了, 那么从指定的scope中获取
    return (T) pageContext.getAttribute(attributeName, TagUtils.getScope(scope));
}