Example usage for javax.servlet.jsp JspContext getAttribute

List of usage examples for javax.servlet.jsp JspContext getAttribute

Introduction

In this page you can find the example usage for javax.servlet.jsp JspContext getAttribute.

Prototype


abstract public Object getAttribute(String name, int scope);

Source Link

Document

Return the object associated with the name in the specified scope or null if not found.

Usage

From source file:org.shredzone.commons.taglib.proxy.AbstractTagProxy.java

/**
 * Gets the {@link BeanFactory} from the given {@link JspContext}. The default
 * implementation automagically finds a {@link BeanFactory} that was previously set by
 * a {@link FrameworkServlet}. The result is cached.
 *
 * @param jspContext//from  w w  w.j a va 2  s . com
 *            {@link JspContext} to be used
 * @return {@link BeanFactory} found
 */
@SuppressWarnings("unchecked")
protected BeanFactory getBeanFactory(JspContext jspContext) {
    Object bfCache = jspContext.getAttribute(TAGPROXY_BEANFACTORY_CACHE, PageContext.APPLICATION_SCOPE);
    if (bfCache != null && bfCache instanceof BeanFactory) {
        return (BeanFactory) bfCache;
    }

    Enumeration<String> en = jspContext.getAttributeNamesInScope(PageContext.APPLICATION_SCOPE);
    while (en.hasMoreElements()) {
        String attribute = en.nextElement();
        if (attribute.startsWith(FrameworkServlet.SERVLET_CONTEXT_PREFIX)) {
            Object bf = jspContext.getAttribute(attribute, PageContext.APPLICATION_SCOPE);
            if (bf != null && bf instanceof BeanFactory) {
                BeanFactory bfBean = (BeanFactory) bf;
                jspContext.setAttribute(TAGPROXY_BEANFACTORY_CACHE, bfBean, PageContext.APPLICATION_SCOPE);
                return bfBean;
            }
        }
    }

    throw new IllegalStateException(
            "Could not find a BeanFactory. Use a FrameworkServlet or @BeanFactoryReference.");
}