Example usage for javax.servlet.jsp JspContext setAttribute

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

Introduction

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

Prototype


abstract public void setAttribute(String name, Object value, int scope);

Source Link

Document

Register the name and value specified with appropriate scope semantics.

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   www  .  ja v  a  2s.c  o  m
 *            {@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.");
}