struts2 hibernate 在jsp页面上面显示多个图片
2014-05-06 11:01
549 查看
最近需要在一个jsp页面上面遍历出多个图片,平时做的比较多的是string integer等类型的遍历,图片是二进制的与原来常用的类型有很大的不同。
BBFSeal.java
public class BBFSeal {
private Blob content;
private String id;
private String name;
private BBFUseruser;
private Blob imageSeal;
public BBFSeal()
{
}
public BBFSeal(Blob imageSeal, String id) {
super();
this.imageSeal = imageSeal;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setContent(Blob content) {
this.content = content;
}
public Blob getContent() {
return content;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setUser(BBFUser user) {
this.user = user;
}
public BBFUser getUser() {
return user;
}
public Blob getImageSeal() {
return imageSeal;
}
public void setImageSeal(Blob imageSeal) {
this.imageSeal = imageSeal;
}
}
action中的主要代码
/**
* 根据用户id 找到所有的印章
*/
public String showSealList() {
HttpServletRequest request = ServletActionContext.getRequest();
BBFUser user = (BBFUser) request.getSession().getAttribute("user");
String uid = user.getId();
this.bbfSeals = this.manage.listSeals(uid);
return "toSealList";
}
/**
* 显示照片信息
*/
public String showSeal() {
BBFSeal seal = manage.loadSeal(id);
// 实例化字节数组流,存储表中的照片字节
inputStream = new ByteArrayInputStream(BlobToBinaryUtils
.blobToBinary(seal.getImageSeal()));
return "success";
}
struts.xml
<action name="sealselect" method="showSealList"
class="sealSelect">
<result name="toSealList">toseal.jsp</result>
</action>
<action name="showSeal" method="showSeal"
class="sealSelect">
<result name="success" type="stream">
<!-- type="stream"用于输出二进制流在文件的下载中会用到
<param name="contentType"></param>
--><param name="bufferSize">1024</param>
</result>
</action>
toseal.jsp 核心代码
</tr>
<s:iterator value="bbfSeals">
<tr >
<td><a data-role="button" data-mini="true" value='id'/>');">
<!-- 显示照片信息, src 指定action ,并传入 ID -->
<img src="showSeal.action?id=<s:property value='id'/>"
</a></td>
</tr>
</s:iterator>
<tr>
public class BlobToBinaryUtils {
public static byte[] blobToBinary(Blob blob) {
InputStream is = null;
byte[] b = null;
try {
is = blob.getBinaryStream();
b = new byte[(int) blob.length()];
is.read(b);
return b;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
is = null;
} catch (Exception e) {
e.printStackTrace();
}
}
return b;
}
}
BBFSeal.java
public class BBFSeal {
private Blob content;
private String id;
private String name;
private BBFUseruser;
private Blob imageSeal;
public BBFSeal()
{
}
public BBFSeal(Blob imageSeal, String id) {
super();
this.imageSeal = imageSeal;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setContent(Blob content) {
this.content = content;
}
public Blob getContent() {
return content;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setUser(BBFUser user) {
this.user = user;
}
public BBFUser getUser() {
return user;
}
public Blob getImageSeal() {
return imageSeal;
}
public void setImageSeal(Blob imageSeal) {
this.imageSeal = imageSeal;
}
}
action中的主要代码
/**
* 根据用户id 找到所有的印章
*/
public String showSealList() {
HttpServletRequest request = ServletActionContext.getRequest();
BBFUser user = (BBFUser) request.getSession().getAttribute("user");
String uid = user.getId();
this.bbfSeals = this.manage.listSeals(uid);
return "toSealList";
}
/**
* 显示照片信息
*/
public String showSeal() {
BBFSeal seal = manage.loadSeal(id);
// 实例化字节数组流,存储表中的照片字节
inputStream = new ByteArrayInputStream(BlobToBinaryUtils
.blobToBinary(seal.getImageSeal()));
return "success";
}
struts.xml
<action name="sealselect" method="showSealList"
class="sealSelect">
<result name="toSealList">toseal.jsp</result>
</action>
<action name="showSeal" method="showSeal"
class="sealSelect">
<result name="success" type="stream">
<!-- type="stream"用于输出二进制流在文件的下载中会用到
<param name="contentType"></param>
--><param name="bufferSize">1024</param>
</result>
</action>
toseal.jsp 核心代码
</tr>
<s:iterator value="bbfSeals">
<tr >
<td><a data-role="button" data-mini="true" value='id'/>');">
<!-- 显示照片信息, src 指定action ,并传入 ID -->
<img src="showSeal.action?id=<s:property value='id'/>"
</a></td>
</tr>
</s:iterator>
<tr>
public class BlobToBinaryUtils {
public static byte[] blobToBinary(Blob blob) {
InputStream is = null;
byte[] b = null;
try {
is = blob.getBinaryStream();
b = new byte[(int) blob.length()];
is.read(b);
return b;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
is = null;
} catch (Exception e) {
e.printStackTrace();
}
}
return b;
}
}
相关文章推荐
- struts2中使用Blob类型处理图片上传保存在数据库中并在JSP页面中显示图片
- Java通过IO流读取图片并显示在JSP页面上面
- JSP页面使用文件上传图片并且回传显示的注意事项
- Struts2的Action向JSP传输List集合并且将数据显示在页面上
- 关于如何实现在整合struts2和hibernate4框架下实现many-to-one级联数据表在JSP页面中的输出
- struts2从数据库中读取mysql的Blob格式图片和sqlserver中image格式图片显示到jsp上
- 将JSP在内存生成的图片显示到页面
- jsp页面不能显示图片问题
- J2EE使用hibernate存储和显示BLOB图片(从页面到数据库)
- jsp页面显示半年内的日期,两行,上面年月,下面日
- Hibernate 对数据库Blob图片的查询操作---页面显示图片,文件的输出
- java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)
- JSP 页面中用绝对路径显示图片
- hibernate struts spring 当showgoods.jsp页面首次打开时显示数据
- 从Oracle中数据库中读取以Blob类型存储的图片显示在jsp页面中
- 将JSP在内存生成的图片显示到页面
- JSP页面直接显示Blob类型图片
- 利用JSP在Web页面中显示随机数字验证码图片
- jsp页面标题前面的图片显示
- java以Blob形式存储,读取图片并在jsp页面显示图片流