📄 jspruntimelibrary.java
字号:
method.invoke(bean, new Object[] { new Long(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
public static void handleSetProperty(Object bean, String prop,
double value)
throws JasperException
{
try {
Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Double(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
public static void handleSetProperty(Object bean, String prop,
float value)
throws JasperException
{
try {
Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Float(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
public static void handleSetProperty(Object bean, String prop,
char value)
throws JasperException
{
try {
Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Character(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
public static void handleSetProperty(Object bean, String prop,
byte value)
throws JasperException
{
try {
Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Byte(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
public static void handleSetProperty(Object bean, String prop,
boolean value)
throws JasperException
{
try {
Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Boolean(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
public static Method getWriteMethod(Class beanClass, String prop)
throws JasperException {
Method method = null;
Class type = null;
try {
java.beans.BeanInfo info
= java.beans.Introspector.getBeanInfo(beanClass);
if ( info != null ) {
java.beans.PropertyDescriptor pd[]
= info.getPropertyDescriptors();
for (int i = 0 ; i < pd.length ; i++) {
if ( pd[i].getName().equals(prop) ) {
method = pd[i].getWriteMethod();
type = pd[i].getPropertyType();
break;
}
}
} else {
// just in case introspection silently fails.
throw new JasperException(
Localizer.getMessage("jsp.error.beans.nobeaninfo",
beanClass.getName()));
}
} catch (Exception ex) {
throw new JasperException (ex);
}
if (method == null) {
if (type == null) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.noproperty",
prop,
beanClass.getName()));
} else {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.nomethod.setproperty",
prop,
type.getName(),
beanClass.getName()));
}
}
return method;
}
public static Method getReadMethod(Class beanClass, String prop)
throws JasperException {
Method method = null;
Class type = null;
try {
java.beans.BeanInfo info
= java.beans.Introspector.getBeanInfo(beanClass);
if ( info != null ) {
java.beans.PropertyDescriptor pd[]
= info.getPropertyDescriptors();
for (int i = 0 ; i < pd.length ; i++) {
if ( pd[i].getName().equals(prop) ) {
method = pd[i].getReadMethod();
type = pd[i].getPropertyType();
break;
}
}
} else {
// just in case introspection silently fails.
throw new JasperException(
Localizer.getMessage("jsp.error.beans.nobeaninfo",
beanClass.getName()));
}
} catch (Exception ex) {
throw new JasperException (ex);
}
if (method == null) {
if (type == null) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.noproperty", prop,
beanClass.getName()));
} else {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.nomethod", prop,
beanClass.getName()));
}
}
return method;
}
//*********************************************************************
// PropertyEditor Support
public static Object getValueFromBeanInfoPropertyEditor(
Class attrClass, String attrName, String attrValue,
Class propertyEditorClass)
throws JasperException
{
try {
PropertyEditor pe = (PropertyEditor)propertyEditorClass.newInstance();
pe.setAsText(attrValue);
return pe.getValue();
} catch (Exception ex) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.property.conversion",
attrValue, attrClass.getName(), attrName,
ex.getMessage()));
}
}
public static Object getValueFromPropertyEditorManager(
Class attrClass, String attrName, String attrValue)
throws JasperException
{
try {
PropertyEditor propEditor =
PropertyEditorManager.findEditor(attrClass);
if (propEditor != null) {
propEditor.setAsText(attrValue);
return propEditor.getValue();
} else {
throw new IllegalArgumentException(
Localizer.getMessage("jsp.error.beans.propertyeditor.notregistered"));
}
} catch (IllegalArgumentException ex) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.property.conversion",
attrValue, attrClass.getName(), attrName,
ex.getMessage()));
}
}
// ************************************************************************
// General Purpose Runtime Methods
// ************************************************************************
/**
* Convert a possibly relative resource path into a context-relative
* resource path that starts with a '/'.
*
* @param request The servlet request we are processing
* @param relativePath The possibly relative resource path
*/
public static String getContextRelativePath(ServletRequest request,
String relativePath) {
if (relativePath.startsWith("/"))
return (relativePath);
if (!(request instanceof HttpServletRequest))
return (relativePath);
HttpServletRequest hrequest = (HttpServletRequest) request;
String uri = (String)
request.getAttribute("javax.servlet.include.servlet_path");
if (uri != null) {
String pathInfo = (String)
request.getAttribute("javax.servlet.include.path_info");
if (pathInfo == null) {
if (uri.lastIndexOf('/') >= 0)
uri = uri.substring(0, uri.lastIndexOf('/'));
}
}
else {
uri = hrequest.getServletPath();
if (uri.lastIndexOf('/') >= 0)
uri = uri.substring(0, uri.lastIndexOf('/'));
}
return uri + '/' + relativePath;
}
/**
* Perform a RequestDispatcher.include() operation, with optional flushing
* of the response beforehand.
*
* @param request The servlet request we are processing
* @param response The servlet response we are processing
* @param relativePath The relative path of the resource to be included
* @param out The Writer to whom we are currently writing
* @param flush Should we flush before the include is processed?
*
* @exception IOException if thrown by the included servlet
* @exception ServletException if thrown by the included servlet
*/
public static void include(ServletRequest request,
ServletResponse response,
String relativePath,
JspWriter out,
boolean flush)
throws IOException, ServletException {
if (flush && !(out instanceof BodyContent))
out.flush();
// FIXME - It is tempting to use request.getRequestDispatcher() to
// resolve a relative path directly, but Catalina currently does not
// take into account whether the caller is inside a RequestDispatcher
// include or not. Whether Catalina *should* take that into account
// is a spec issue currently under review. In the mean time,
// replicate Jasper's previous behavior
String resourcePath = getContextRelativePath(request, relativePath);
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
rd.include(request,
new ServletResponseWrapperInclude(response, out));
}
/**
* URL encodes a string, based on the supplied character encoding.
* This performs the same function as java.next.URLEncode.encode
* in J2SDK1.4, and should be removed if the only platform supported
* is 1.4 or higher.
* @param s The String to be URL encoded.
* @param enc The character encoding
* @return The URL encoded String
*/
public static String URLEncode(String s, String enc) {
if (s == null) {
return "null";
}
if (enc == null) {
enc = "ISO-8859-1"; // The default request encoding
}
StringBuffer out = new StringBuffer(s.length());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(buf, enc);
} catch (java.io.UnsupportedEncodingException ex) {
// Use the default encoding?
writer = new OutputStreamWriter(buf);
}
for (int i = 0; i < s.length(); i++) {
int c = s.charAt(i);
if (c == ' ') {
out.append('+');
} else if (isSafeChar(c)) {
out.append((char)c);
} else {
// convert to external encoding before hex conversion
try {
writer.write(c);
writer.flush();
} catch(IOException e) {
buf.reset();
continue;
}
byte[] ba = buf.toByteArray();
for (int j = 0; j < ba.length; j++) {
out.append('%');
// Converting each byte in the buffer
out.append(Character.forDigit((ba[j]>>4) & 0xf, 16));
out.append(Character.forDigit(ba[j] & 0xf, 16));
}
buf.reset();
}
}
return out.toString();
}
private static boolean isSafeChar(int c) {
if (c >= 'a' && c <= 'z') {
return true;
}
if (c >= 'A' && c <= 'Z') {
return true;
}
if (c >= '0' && c <= '9') {
return true;
}
if (c == '-' || c == '_' || c == '.' || c == '!' ||
c == '~' || c == '*' || c == '\'' || c == '(' || c == ')') {
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -