[英]jstl - how do i pass an expression language variable to a javascript alert?
我試圖使管理頁面連接到servlet,在其中可以將一些元素插入數據庫。 這是音樂會門票預訂網站的演示,我使用表格輸入數據,並使用servlet對其進行驗證。
如果servlet在表單的一個或多個字段中發現無效數據,它將以格式字符串形式返回自定義消息mismatchOut-每行一條無效消息,例如:
Concert date must be in yyyy-mm-dd format!
start time must be in hh:mm:ss format!
我在我的jsp中將其傳遞為:
<c:set var="invalidConcertInputMessage" value="${mismatchOut}" scope="page" />
我要做的是將此消息傳遞給警報,警報將向管理員用戶顯示該消息。
我試過了:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert("${invalidConcertInputMessage}");
</script>
</c:if>
,
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert(${invalidConcertInputMessage});
</script>
</c:if>
,
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert(<c:out value="${invalidConcertInputMessage}");
</script>
</c:if>
,
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert("<c:out value="${invalidConcertInputMessage}" />");
</script>
</c:if>
和:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert('<c:out value="${invalidConcertInputMessage}" />');
</script>
</c:if>
似乎沒有任何效果。 我可能在這里錯過了sintax,因為當涉及UI時,我是一個新手。
這可以正常工作並打印出警告,提示“某些文本”:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert("some text");
</script>
</c:if>
請告訴我如何將自定義消息傳遞給警報。 還是有可能嗎?
看來問題在於我將換行符以\\n
發布到我的自定義消息字符串中,但是java將它們解析為自己的換行符,而不是僅僅將\\n
發送到Java腳本中,這會導致某種形式的無效字符串,不解析。
我通過在Servlet發送的自定義字符串\\n with
\\ n`替換\\n with
解決此問題。
對於這個困境中的其他人,這是我使用的語法:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
var message = "${invalidConcertInputMessage}";
alert(message);
</script>
</c:if>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.