struts2前端頁面讀取Clob BLOB

原文

  http://blog.csdn.net/shanhuhau/article/details/38794671

在通過Struts2標籤顯示對象的Clob屬性值的時候,顯示的並不是CLOB或者BLOB的內容,而是顯示的toString方法的值

例如我在實體中的註解爲:

@Lob
  @Column(name = "CONTENT_TEXT")
  public String getContentText() {
    return contentText;
  }
前臺頁面讀取方式爲:
<s:property value="#entry.contentText" />
顯示結果爲:
oracle.sql.CLOB@1077e76

要想正常顯示CLOB或者BLOB的內容。需要在action中加入對clob或者blob的轉換方法

public String getClob(Clob c){
    Reader reader = null;
    StringBuffer sb = new StringBuffer();
    try {
      reader = c.getCharacterStream();
      BufferedReader br = new BufferedReader(reader);
      String temp = null;
      while ((temp=br.readLine()) != null) {
        sb.append(temp);
      }
    } catch (Exception e) {
      
    }finally{
      if (reader!=null) {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
    } 	
    return sb.toString();
  }
前端頁面調用改爲
<s:property value="%{getClob(#entry.contentText)}" /></span>
這樣就可以正常顯示clob的內容了,blob類似,只是把讀取方式換成二進制流讀取
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章