86.如何将数据库中的CLOB格式输出到JSP页面
2017-04-14 16:04
495 查看
1.数据库字段存储的CLOB类型字段
2.实体Bean中是Object类型对应数据库CLOB字段
3.控制层发送到JSP页面为对象
首先编写ClobToString.java类
/**
* File Name: ClobToString.java
*
* File Desc: 处理clob大对象类型
*
* Product AB: WEB_1_0_0
*
* Product Name: 网站
*
* Author: kan.jiang
*
* History: 2010-9-14 created by kan.jiang
*/
package com.saiyou.util;
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* 处理clob大对象类型
*
* @author DarrenMJ
* @version 1.0
* @date 2017-4-14 9:55:33
*/
public class ClobToString 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) {
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());
}
}
}
其次编写ClobTag.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.saiyou.util.ClobToString</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>
需要注意的是,要对应的ClobToString.java文件所在路径(如图)
<tag-class>com.saiyou.util.ClobToString</tag-class>
然后到JSP页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="p" uri="tld/ClobTag.tld"%>
。。。
<div><p:colb clob="${item.description}" n="45" addContent="..."/></div>
要注意的是<%@taglib prefix="p" uri="tld/ClobTag.tld"%> 写你tld所在位置 ,如果不确定对不对,按住CTRL键 鼠标左键点一下,看能不不能点进去(如图)
2.实体Bean中是Object类型对应数据库CLOB字段
3.控制层发送到JSP页面为对象
首先编写ClobToString.java类
/**
* File Name: ClobToString.java
*
* File Desc: 处理clob大对象类型
*
* Product AB: WEB_1_0_0
*
* Product Name: 网站
*
* Author: kan.jiang
*
* History: 2010-9-14 created by kan.jiang
*/
package com.saiyou.util;
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* 处理clob大对象类型
*
* @author DarrenMJ
* @version 1.0
* @date 2017-4-14 9:55:33
*/
public class ClobToString 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) {
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());
}
}
}
其次编写ClobTag.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.saiyou.util.ClobToString</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>
需要注意的是,要对应的ClobToString.java文件所在路径(如图)
<tag-class>com.saiyou.util.ClobToString</tag-class>
然后到JSP页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="p" uri="tld/ClobTag.tld"%>
。。。
<div><p:colb clob="${item.description}" n="45" addContent="..."/></div>
要注意的是<%@taglib prefix="p" uri="tld/ClobTag.tld"%> 写你tld所在位置 ,如果不确定对不对,按住CTRL键 鼠标左键点一下,看能不不能点进去(如图)
相关文章推荐
- 如何把aspx页面中的某个控件输出成html页面(例如把一个页面中的datagrid数据输出成html格式的页面保存)
- 如何打开jsp页面时经过action从数据库取得数据显示在页面上
- 在JSP页面中输出JSON格式数据
- 如何把数据库中的String类型的数据在jsp页面上转化为date类型的数据
- JSP页面用EL表达式 输出date格式
- 浅谈如何调用JSP读取数据库中图片并显示在页面上
- 在JSP页面中输出JSON格式数据
- JSP中如何 循环显示 数据库中的数据到list.jsp页面 (转载)
- 在JSP页面中输出JSON格式数据
- jsp采用数据库连接池的方法获取数据库时间戳context.xml配置,jsp页面把时间格式化成自己需要的格式
- 请问如何在Web页面中点击一个button之后,用jsp从数据库中读取数据显示到表格里
- 如何在JSP页面快速输出从Servlet接收的数据_看JSTL常用C标签的威力
- spring mvc 在jsp页面如何使时间以格式yyyy-MM-dd HH:mm:ss显示,24小时制
- 在jsp页面上格式化输出日期格式
- 如何在 jsp 上输出数据库中保存的文本
- 如何删除strut2校验后<s:fielderror>在jsp页面输出时的小黑点和自动换行?
- 将JSP页面内容作为Excel格式输出方法一例
- 在JSP页面中输出JSON格式数据
- JSP页面用EL表达式 输出date格式
- 在jsp页面如何转换日期格式