本文整理汇总了Java中javax.servlet.jsp.JspContext.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java JspContext.setAttribute方法的具体用法?Java JspContext.setAttribute怎么用?Java JspContext.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.jsp.JspContext
的用法示例。
在下文中一共展示了JspContext.setAttribute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBeanFactory
import javax.servlet.jsp.JspContext; //导入方法依赖的package包/类
/**
* 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
* {@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.");
}
示例2: doTag
import javax.servlet.jsp.JspContext; //导入方法依赖的package包/类
@Override
public void doTag () throws JspException, IOException
{
final JspFragment body = (JspFragment)getJspContext ().getAttribute ( DefineTag.ATTR_PREFIX + this.name );
if ( body == null )
{
throw new JspException ( String.format ( "Unable to find macro '%s'", this.name ) );
}
final JspContext ctx = body.getJspContext ();
// set attributes to body context
final Map<String, Object> oldEntries = new HashMap<> ( this.data.size () );
for ( final Map.Entry<String, Object> entry : this.data.entrySet () )
{
oldEntries.put ( entry.getKey (), ctx.getAttribute ( entry.getKey (), PageContext.PAGE_SCOPE ) );
ctx.setAttribute ( entry.getKey (), entry.getValue (), PageContext.PAGE_SCOPE );
}
// invoke
body.invoke ( getJspContext ().getOut () );
// set old values, so we don't clutter up the context for the next caller
for ( final String key : this.data.keySet () )
{
final Object val = oldEntries.get ( key );
if ( val == null )
{
ctx.removeAttribute ( key, PageContext.PAGE_SCOPE );
}
else
{
ctx.setAttribute ( key, val, PageContext.PAGE_SCOPE );
}
}
}
示例3: getUid
import javax.servlet.jsp.JspContext; //导入方法依赖的package包/类
public static String getUid(JspContext context) {
String uuid = null;
do {
long rand = Double.doubleToLongBits(Math.random());
uuid = Long.toHexString(rand).substring(0, 4);
} while (context.getAttribute(uuid) != null);
context.setAttribute(uuid, uuid);
return uuid;
}