?? requestutils.java
字號:
package com.ly.tag;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.apache.commons.beanutils.PropertyUtils;
/** *//**
* @author ceun
*
*/
public class RequestUtils {
private static Map scopes = new HashMap();
static {
scopes.put("page", new Integer(PageContext.PAGE_SCOPE));
scopes.put("request", new Integer(PageContext.REQUEST_SCOPE));
scopes.put("session", new Integer(PageContext.SESSION_SCOPE));
scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE));
}
/** *//**
* Locate and return the specified bean, from an optionally specified
* scope, in the specified page context. If no such bean is found,
* return <code>null</code> instead. If an exception is thrown, it will
* have already been saved via a call to <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param scopeName Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
* @return JavaBean in the specified page context
* @exception JspException if an invalid scope name
* is requested
* @since ceunUtil 1.2.1
*/
public static Object lookup(PageContext pageContext, String name, String scopeName)
throws JspException {
if (scopeName == null) {
return pageContext.findAttribute(name);
}
try {
return pageContext.getAttribute(name, getScope(scopeName));
} catch (JspException e) {
saveException(pageContext, e);
throw e;
}
}
/** *//**
* Converts the scope name into its corresponding PageContext constant value.
* @param scopeName Can be "page", "request", "session", or "application" in any
* case.
* @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
* @throws JspException if the scopeName is not a valid name.
* @since ceunUtil 1.2.1
*/
public static int getScope(String scopeName) throws JspException {
Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
if (scope == null) {
throw new JspException("未指定范圍");
}
return scope.intValue();
}
/** *//**
* Locate and return the specified property of the specified bean, from
* an optionally specified scope, in the specified page context. If an
* exception is thrown, it will have already been saved via a call to
* <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param property Name of the property to be retrieved, or
* <code>null</code> to retrieve the bean itself
* @param scope Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
* @return property of specified JavaBean
*
* @exception JspException if an invalid scope name
* is requested
* @exception JspException if the specified bean is not found
* @exception JspException if accessing this property causes an
* IllegalAccessException, IllegalArgumentException,
* InvocationTargetException, or NoSuchMethodException
*/
public static Object lookup(
PageContext pageContext,
String name,
String property,
String scope)
throws JspException {
// Look up the requested bean, and return if requested
Object bean = lookup(pageContext, name, scope);
if (bean == null) {
JspException e = null;
if (scope == null) {
e = new JspException("在指定范圍內找不到對象");
} else {
e = new JspException("找不到對象");
}
saveException(pageContext, e);
throw e;
}
if (property == null) {
return bean;
}
// Locate and return the specified property
try {
return PropertyUtils.getProperty(bean, property);
} catch (IllegalAccessException e) {
saveException(pageContext, e);
throw new JspException("不能訪問指定對象的屬性");
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null) {
t = e;
}
saveException(pageContext, t);
throw new JspException("找不到對象");
} catch (NoSuchMethodException e) {
saveException(pageContext, e);
throw new JspException("找不到此方法");
}
}
public static void saveException(PageContext pageContext, Throwable exception) {
pageContext.setAttribute("ceun.tag.error", exception, PageContext.REQUEST_SCOPE);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -