在 JSTL 'form field' 上调用 javascript 函数

calling a javascript function on a JSTL 'form field'

我正在尝试用 <br /> 标签替换一些换行符。我不确定如何调用我的函数?基本上在头脑中我有这样的功能

<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>

任何人都可以告诉我如何实现这一点。看起来应该很简单,但也许我错过了重点,因为我尝试了很多方法,但都失败了!?

您可以使用标签库来获得相同的结果:

       <%@ 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>