2017-02-10 22 views
0

我試圖用<br />標記替換一些換行符。我不知道如何調用我的功能?基本上在頭我有這樣在JSTL「表單字段」上調用javascript函數

<head> 
    <script> 
     function replaceLineFeedsFunction (param){ 
      return param.replace(/\n/g,"<br />"); 
      } 
    </script> 
</head> 
頁面我有一個這樣的部分(這是精簡到裸露的骨頭,所以我希望它仍然使一些技術意義上的)

<c:forEach items="${supplier.accounts}" var="supacc"> 
    <table id="${supacc.id}"> 
     <tr> 
      <td>Account Title</td> 
      <td>${supacc.title}</td> 
     </tr> 
    </table> 
</c:forEach> 

功能

它是在$ {} supacc.title的文字,我想給函數適用於作爲該領域呈現......因此,像這樣

<c:forEach items="${supplier.accounts}" var="supacc"> 
    <table id="${supacc.id}"> 
     <tr> 
      <td>Account Title</td> 
      <td>replaceLineFeedsFunction(${supacc.title})</td> 
     </tr> 
    </table> 
</c:forEach> 

誰能告訴我如何實現這一目標。它似乎應該是如此簡單,但也許我錯過了這一點,因爲我已經嘗試了很多方法,所有這些方法都失敗了!?

回答

1

您可以使用標籤庫來實現相同的結果:

 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>  
     <c:forEach items="${supplier.accounts}" var="supacc"> 
     <table id="${supacc.id}"> 
      <tr> 
       <td>Account Title</td> 
       <td>${fn:replace(supacc.title, '\n', '<br />')}</td> 
      </tr> 
     </table> 
     </c:forEach> 
相關問題