下面列出了怎么用javax.servlet.jsp.JspContext的API类实例代码及写法,或者点击链接到github查看源代码。
public JspContextWrapper(JspTag jspTag, JspContext jspContext,
ArrayList<String> nestedVars, ArrayList<String> atBeginVars,
ArrayList<String> atEndVars, Map<String,String> aliases) {
this.jspTag = jspTag;
this.invokingJspCtxt = (PageContext) jspContext;
if (jspContext instanceof JspContextWrapper) {
rootJspCtxt = ((JspContextWrapper)jspContext).rootJspCtxt;
}
else {
rootJspCtxt = invokingJspCtxt;
}
this.nestedVars = nestedVars;
this.atBeginVars = atBeginVars;
this.atEndVars = atEndVars;
this.pageAttributes = new HashMap<>(16);
this.aliases = aliases;
if (nestedVars != null) {
this.originalNestedVars = new HashMap<>(nestedVars.size());
} else {
this.originalNestedVars = null;
}
syncBeginTagFile();
}
@Override
public void setValue(ELContext context, Object base, Object property, Object value) {
Objects.requireNonNull(context);
if (base == null) {
context.setPropertyResolved(base, property);
if (property != null) {
String key = property.toString();
PageContext page = (PageContext) context.getContext(JspContext.class);
int scope = page.getAttributesScope(key);
if (scope != 0) {
page.setAttribute(key, value, scope);
} else {
page.setAttribute(key, value);
}
}
}
}
public JspContextWrapper(JspContext jspContext,
ArrayList<String> nestedVars, ArrayList<String> atBeginVars,
ArrayList<String> atEndVars, Map<String,String> aliases) {
this.invokingJspCtxt = (PageContext) jspContext;
if (jspContext instanceof JspContextWrapper) {
rootJspCtxt = ((JspContextWrapper)jspContext).rootJspCtxt;
}
else {
rootJspCtxt = invokingJspCtxt;
}
this.nestedVars = nestedVars;
this.atBeginVars = atBeginVars;
this.atEndVars = atEndVars;
this.pageAttributes = new HashMap<String, Object>(16);
this.aliases = aliases;
if (nestedVars != null) {
this.originalNestedVars = new HashMap<String, Object>(nestedVars.size());
}
syncBeginTagFile();
}
@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;
}
@Override
public void setValue(ELContext context, Object base, Object property,
Object value) throws NullPointerException,
PropertyNotFoundException, PropertyNotWritableException,
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);
int scope = page.getAttributesScope(key);
if (scope != 0) {
page.setAttribute(key, value, scope);
} else {
page.setAttribute(key, value);
}
}
}
}
public JspContextWrapper(JspContext jspContext,
ArrayList<String> nestedVars, ArrayList<String> atBeginVars,
ArrayList<String> atEndVars, Map<String,String> aliases) {
this.invokingJspCtxt = (PageContext) jspContext;
if (jspContext instanceof JspContextWrapper) {
rootJspCtxt = ((JspContextWrapper)jspContext).rootJspCtxt;
}
else {
rootJspCtxt = invokingJspCtxt;
}
this.nestedVars = nestedVars;
this.atBeginVars = atBeginVars;
this.atEndVars = atEndVars;
this.pageAttributes = new HashMap<String, Object>(16);
this.aliases = aliases;
if (nestedVars != null) {
this.originalNestedVars = new HashMap<String, Object>(nestedVars.size());
}
syncBeginTagFile();
}
@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;
}
@Override
public void setValue(ELContext context, Object base, Object property,
Object value) throws NullPointerException,
PropertyNotFoundException, PropertyNotWritableException,
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);
int scope = page.getAttributesScope(key);
if (scope != 0) {
page.setAttribute(key, value, scope);
} else {
page.setAttribute(key, value);
}
}
}
}
public JspContextWrapper(JspContext jspContext,
ArrayList<String> nestedVars,
ArrayList<String> atBeginVars,
ArrayList<String> atEndVars,
Map<String, String> aliases) {
this.invokingJspCtxt = (PageContext) jspContext;
this.nestedVars = nestedVars;
this.atBeginVars = atBeginVars;
this.atEndVars = atEndVars;
this.pageAttributes = new Hashtable<String, Object>(16);
this.aliases = aliases;
if (nestedVars != null) {
this.originalNestedVars =
new HashMap<String, Object>(nestedVars.size());
}
syncBeginTagFile();
}
/**
* パンクズリストを表示します。
* このメソッドはJSP-BODYを評価せずに、
* 既存のHTMLテンプレート、あるいはクラスコンフィグオーバライドされたHTMLテンプレートを利用します。
* @throws JspException JSP処理中にエラーが発生した場合
* @throws IOException 入出力エラーの場合
*/
protected void render() throws JspException, IOException {
render(new JspFragment() {
@Override public JspContext getJspContext() {
return PankuzuTag.this.getJspContext();
}
@Override public void invoke(Writer out) throws JspException, IOException {
JspContext context = getJspContext();
ViewId vid = (ViewId) context.getAttribute(var);
String encodedUrl = encodeURL(vid.getUrl());
String item = Strings.substitute(DEFAULT_TEMPLATE, Maps
.hash("url", encodedUrl)
.map("label", Strings.escapeHTML(vid.getLabel()))
.map("query", trimQueryMarkerIfGetRequestParameterExists(encodedUrl, vid.getQuery())));
context.getOut().print(item);
}
});
}
@Override
public Object getContext(@SuppressWarnings("rawtypes") Class key) {
if (key == JspContext.class) {
return pageContext;
}
return wrapped.getContext(key);
}
public JspFragmentHelper( int discriminator, JspContext jspContext,
JspTag parentTag )
{
this.discriminator = discriminator;
this.jspContext = jspContext;
if(jspContext instanceof PageContext) {
_jspx_page_context = (PageContext)jspContext;
} else {
_jspx_page_context = null;
}
this.parentTag = parentTag;
}
@Override
public Object getValue(ELContext context, Object base, Object property) {
Objects.requireNonNull(context);
if (base == null && property != null) {
int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
if (idx >= 0) {
PageContext page = (PageContext) context
.getContext(JspContext.class);
context.setPropertyResolved(base, property);
switch (idx) {
case APPLICATIONSCOPE:
return ScopeManager.get(page).getApplicationScope();
case COOKIE:
return ScopeManager.get(page).getCookie();
case HEADER:
return ScopeManager.get(page).getHeader();
case HEADERVALUES:
return ScopeManager.get(page).getHeaderValues();
case INITPARAM:
return ScopeManager.get(page).getInitParam();
case PAGECONTEXT:
return ScopeManager.get(page).getPageContext();
case PAGESCOPE:
return ScopeManager.get(page).getPageScope();
case PARAM:
return ScopeManager.get(page).getParam();
case PARAM_VALUES:
return ScopeManager.get(page).getParamValues();
case REQUEST_SCOPE:
return ScopeManager.get(page).getRequestScope();
case SESSION_SCOPE:
return ScopeManager.get(page).getSessionScope();
}
}
}
return null;
}
@Override
public Object getContext(@SuppressWarnings("rawtypes") Class key) {
if (key == JspContext.class) {
return pageContext;
}
return wrapped.getContext(key);
}
@Override
public Object getContext(@SuppressWarnings("rawtypes") Class key) {
if (key == JspContext.class) {
return pageContext;
}
return wrapped.getContext(key);
}
public ELContext getELContext() {
if (elContext == null) {
PageContext pc = invokingJspCtxt;
while (pc instanceof JspContextWrapper) {
pc = ((JspContextWrapper)pc).invokingJspCtxt;
}
PageContextImpl pci = (PageContextImpl) pc;
elContext = pci.getJspApplicationContext().createELContext(
invokingJspCtxt.getELContext().getELResolver());
elContext.putContext(javax.servlet.jsp.JspContext.class, this);
((ELContextImpl)elContext).setVariableMapper(
new VariableMapperImpl());
}
return elContext;
}
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;
}
@Override
public JspContext getJspContext() {
return this.jspContext;
}
@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;
}
public void setJspContext(JspContext arg0) {
this.jspContext = arg0;
}
public void setJspContext(JspContext arg0) {
this.jspContext = arg0;
}
@Override
public JspContext getJspContext() {
return this.jspContext;
}
@Override
public Object getValue(ELContext context, Object base, Object property)
throws NullPointerException, PropertyNotFoundException, ELException {
if (context == null) {
throw new NullPointerException();
}
if (base == null && property != null) {
int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
if (idx >= 0) {
PageContext page = (PageContext) context
.getContext(JspContext.class);
context.setPropertyResolved(true);
switch (idx) {
case APPLICATIONSCOPE:
return ScopeManager.get(page).getApplicationScope();
case COOKIE:
return ScopeManager.get(page).getCookie();
case HEADER:
return ScopeManager.get(page).getHeader();
case HEADERVALUES:
return ScopeManager.get(page).getHeaderValues();
case INITPARAM:
return ScopeManager.get(page).getInitParam();
case PAGECONTEXT:
return ScopeManager.get(page).getPageContext();
case PAGESCOPE:
return ScopeManager.get(page).getPageScope();
case PARAM:
return ScopeManager.get(page).getParam();
case PARAM_VALUES:
return ScopeManager.get(page).getParamValues();
case REQUEST_SCOPE:
return ScopeManager.get(page).getRequestScope();
case SESSION_SCOPE:
return ScopeManager.get(page).getSessionScope();
}
}
}
return null;
}
/**
* 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;
}
/**
* If the base object is <code>null</code>, and the property matches
* the name of a JSP implicit object, returns the implicit object.
*
* <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 an implicit object is matched. 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 implicit object to resolve.
* @return If the <code>propertyResolved</code> property of
* <code>ELContext</code> was set to <code>true</code>, then
* the implicit object; 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) {
return null;
}
PageContext ctxt = (PageContext)context.getContext(JspContext.class);
if ("pageContext".equals(property)) {
context.setPropertyResolved(true);
return ctxt;
}
ImplicitObjects implicitObjects =
ImplicitObjects.getImplicitObjects(ctxt);
if ("pageScope".equals(property)) {
context.setPropertyResolved(true);
return implicitObjects.getPageScopeMap();
}
if ("requestScope".equals(property)) {
context.setPropertyResolved(true);
return implicitObjects.getRequestScopeMap();
}
if ("sessionScope".equals(property)) {
context.setPropertyResolved(true);
return implicitObjects.getSessionScopeMap();
}
if ("applicationScope".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getApplicationScopeMap ();
}
if ("param".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getParamMap();
}
if ("paramValues".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getParamsMap();
}
if ("header".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getHeaderMap();
}
if ("headerValues".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getHeadersMap();
}
if ("initParam".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getInitParamMap ();
}
if ("cookie".equals (property)) {
context.setPropertyResolved(true);
return implicitObjects.getCookieMap ();
}
return null;
}
@Override
public JspContext getJspContext() {
return this.jspContext;
}
@Override
public Object getValue(ELContext context, Object base, Object property)
throws NullPointerException, PropertyNotFoundException, ELException {
if (context == null) {
throw new NullPointerException();
}
if (base == null && property != null) {
int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
if (idx >= 0) {
PageContext page = (PageContext) context
.getContext(JspContext.class);
context.setPropertyResolved(true);
switch (idx) {
case APPLICATIONSCOPE:
return ScopeManager.get(page).getApplicationScope();
case COOKIE:
return ScopeManager.get(page).getCookie();
case HEADER:
return ScopeManager.get(page).getHeader();
case HEADERVALUES:
return ScopeManager.get(page).getHeaderValues();
case INITPARAM:
return ScopeManager.get(page).getInitParam();
case PAGECONTEXT:
return ScopeManager.get(page).getPageContext();
case PAGESCOPE:
return ScopeManager.get(page).getPageScope();
case PARAM:
return ScopeManager.get(page).getParam();
case PARAM_VALUES:
return ScopeManager.get(page).getParamValues();
case REQUEST_SCOPE:
return ScopeManager.get(page).getRequestScope();
case SESSION_SCOPE:
return ScopeManager.get(page).getSessionScope();
}
}
}
return null;
}
public JspContext getJspContext() {
return this.jspContext;
}
@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 );
}
}
}
@Override
public JspContext getJspContext() {
return super.getJspContext();
}