2014-10-20 71 views
1

我想顯示具有圖像的細節。其實可能是一個沒有形象的人。這裏是我的控制器來顯示它如何在春天顯示存儲在數據庫中的圖像mvc

@RequestMapping(value="/Hows/{categoryId}/{categoryName}",method=RequestMethod.GET) 
public String showHowsCategory(@PathVariable("categoryId")int category,Model model,HttpServletRequest request){ 

    int page = 1; 
    if(request.getParameter("page")!=null) 
     page = Integer.parseInt(request.getParameter("page").toString()); 

    ListHows = showCategoryService.showAllHowInCategory(category,(page- 1)*pageSize,pageSize); 
    for(int i=(page-1)*pageSize;i<pageSize;i++){ 
     byte[] binaryData = ListHows.get(i).getImage(); 
     if(binaryData != null){ 
     try { 
       byte[] encodeBase64 = Base64.encode(binaryData); 
       String base64Encoded = new String(encodeBase64, "UTF-8"); 
       imageList.add(base64Encoded); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    model.addAttribute("image", imageList); 
    model.addAttribute("howList",ListHows); 

並在jsp視圖中使用此代碼。但不顯示圖像。

<c:forEach var="how" items="${howList}" varStatus="counter"> 
      <c:set var="url" value="${how.how}"/> 
       <c:set var="count" value="0" /> 
      <div class="table"> 
      <div style="float: left;margin-left: 10px;"> 
      <c:if test="${how.image != null}"> 
         <img src="data:image/jpeg;base64,${image[count]}" width="150" height="120"/> 
          <c:set var="count" value="${count + 1}" /> 
         </c:if> 
         <c:if test="${how.image == null}"> 
         <img width="150" height="120" src="<c:url value="/resources/images/how.jpg" />"/> 
         </c:if> 
         </div> 

我的問題是什麼?

回答

1
<img src="data:image/jpeg;charset=utf-8;base64,${image[count]}" width="150" height="120"/> 

數據將是編碼的base64字符串。

+0

當我使用此model.addAttribute(「image」,imageList.get(2))並在jsp視圖其工作。 – 2014-10-20 22:39:28

+0

是的,我瞭解你的情況,但你在另一個列表中使用foreach。 也許你需要把圖像的數據放入howList。你可以像這樣重現。 當你開始循環時,你的計數總是爲零,所以你總是得到$ {image [0]} – 2014-10-20 22:48:54

+0

,但是通過這個。爲什麼要顯示一個圖像>如何將它放入howList? – 2014-10-20 22:57:37

相關問題