jsp页面中处理Clob类型的数据
jsp页面中处理Clob类型的数据,想了想,还是用标签解决:
在web.xml中配置:
<jsp-config>
<taglib>
<taglib-uri>
/myStringUtils-taglib
<taglib-uri>
<taglib-location>/WEB-INF/myStringUtils-taglib.tld</taglib-location>
</taglib>
</jsp-config>
在dtd中:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myStringUtils-taglib</short-name>
<uri>/myStringUtils-taglib</uri>
<function>
<description>convert Clob to String</description>
<name>convertClob</name>
<function-class>cn.org.gddr.web.utils.StringTag</function-class>
<function-signature>
String convertClob(java.lang.Object)
</function-signature>
</function>
</taglib>
Stringtag.java
public class StringTag {
public static String convertClob(Object clob) throws Exception {
if (clob == null) {
return "";
}
StringBuffer clobString = new StringBuffer();
if (clob instanceof Clob) {
int y;
char ac[] = new char[4096];
Reader reader = ((Clob) clob).getCharacterStream();
while ((y = reader.read(ac, 0, 4096)) != -1) {
clobString.append(new String(ac, 0, y));
}
} else {
clobString.append(clob.toString());
}
return clobString.toString();
}
}