代码如下:
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
*
*
* @description:处理clob大对象类型
* @author:luo
* @creatTime:Jun 19, 2009 4:42:38 PM
* @version
*
*/
public class ClobToStringTag extends SimpleTagSupport {
private Object clob;
private int n;
private String addContent;
public String getAddContent() {
return addContent;
}
public void setAddContent(String addContent) {
this.addContent = addContent;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public Object getClob() {
return clob;
}
public void setClob(Object clob) {
this.clob = clob;
}
@Override
public void doTag() throws JspException, IOException {
JspContext jc = super.getJspContext();
JspWriter out = jc.getOut();
if (clob == null) {
out.print("");
}
StringBuffer clobString = new StringBuffer();
if (clob instanceof Clob) {
int y;
char ac[] = new char[4096];
Reader reader;
try {
reader = ((Clob) clob).getCharacterStream();
while ((y = reader.read(ac, 0, 4096)) != -1) {
clobString.append(new String(ac, 0, y));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
clobString.append(clob.toString());
}
if(clobString.toString().length()>n && n>0)
out.print(clobString.toString().substring(0, n)+addContent);
else
out.print( clobString.toString());
}
}
**********************************************************************
tld配置文件中
<?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">
<description>自定义标签</description>
<display-name>mytag</display-name>
<tlib-version>1.0</tlib-version>
<short-name>tag</short-name>
<uri>http://myDefineTags.com/mytags</uri>
<tag>
<description>输出Colb对象</description>
<name>colb</name>
<tag-class>com.qingdao.tour.action.tag.ClobToStringTag</tag-class>
<body-content>empty</body-content>
<attribute>
<description>需要输出的Colb对象</description>
<name>clob</name>
<required>true</required><!-- 必须出现 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>字符串超过n的时候截取</description>
<name>n</name>
<required>true</required><!-- 必须出现 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>追加内容</description>
<name>addContent</name>
<required>true</required><!-- 必须出现 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
*************************************
jsp页面写法如下
<p:colb clob="${unitpo[20] }" n="45" addContent="..."/>
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
*
*
* @description:处理clob大对象类型
* @author:luo
* @creatTime:Jun 19, 2009 4:42:38 PM
* @version
*
*/
public class ClobToStringTag extends SimpleTagSupport {
private Object clob;
private int n;
private String addContent;
public String getAddContent() {
return addContent;
}
public void setAddContent(String addContent) {
this.addContent = addContent;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public Object getClob() {
return clob;
}
public void setClob(Object clob) {
this.clob = clob;
}
@Override
public void doTag() throws JspException, IOException {
JspContext jc = super.getJspContext();
JspWriter out = jc.getOut();
if (clob == null) {
out.print("");
}
StringBuffer clobString = new StringBuffer();
if (clob instanceof Clob) {
int y;
char ac[] = new char[4096];
Reader reader;
try {
reader = ((Clob) clob).getCharacterStream();
while ((y = reader.read(ac, 0, 4096)) != -1) {
clobString.append(new String(ac, 0, y));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
clobString.append(clob.toString());
}
if(clobString.toString().length()>n && n>0)
out.print(clobString.toString().substring(0, n)+addContent);
else
out.print( clobString.toString());
}
}
**********************************************************************
tld配置文件中
<?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">
<description>自定义标签</description>
<display-name>mytag</display-name>
<tlib-version>1.0</tlib-version>
<short-name>tag</short-name>
<uri>http://myDefineTags.com/mytags</uri>
<tag>
<description>输出Colb对象</description>
<name>colb</name>
<tag-class>com.qingdao.tour.action.tag.ClobToStringTag</tag-class>
<body-content>empty</body-content>
<attribute>
<description>需要输出的Colb对象</description>
<name>clob</name>
<required>true</required><!-- 必须出现 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>字符串超过n的时候截取</description>
<name>n</name>
<required>true</required><!-- 必须出现 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>追加内容</description>
<name>addContent</name>
<required>true</required><!-- 必须出现 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
*************************************
jsp页面写法如下
<p:colb clob="${unitpo[20] }" n="45" addContent="..."/>