[英]jstl - how do i pass an expression language variable to a javascript alert?
I'm trying to make an admin page connected to a servlet, where I can insert some elements into a database. 我试图使管理页面连接到servlet,在其中可以将一些元素插入数据库。 It's a demo of a concert ticket booking website and I use a form to input data, and the servlet to validate it.
这是音乐会门票预订网站的演示,我使用表格输入数据,并使用servlet对其进行验证。
If the servlet finds invalid data in one or more of the form's fields, it returns a custom message as a formatted string named mismatchOut - one invalidity message per line, eg: 如果servlet在表单的一个或多个字段中发现无效数据,它将以格式字符串形式返回自定义消息mismatchOut-每行一条无效消息,例如:
Concert date must be in yyyy-mm-dd format!
start time must be in hh:mm:ss format!
I pass this in my jsp as: 我在我的jsp中将其传递为:
<c:set var="invalidConcertInputMessage" value="${mismatchOut}" scope="page" />
What I want to do is pass this message to an alert, which will show the message to the admin user. 我要做的是将此消息传递给警报,警报将向管理员用户显示该消息。
I've tried: 我试过了:
<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>
and: 和:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert('<c:out value="${invalidConcertInputMessage}" />');
</script>
</c:if>
and nothing seems to work. 似乎没有任何效果。 I'm probably missing the sintax here, as I'm a total newbie when it comes tu UI.
我可能在这里错过了sintax,因为当涉及UI时,我是一个新手。
This works and prints out an alert saying "some text": 这可以正常工作并打印出警告,提示“某些文本”:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
alert("some text");
</script>
</c:if>
Please tell me how I can pass my custom message to the alert. 请告诉我如何将自定义消息传递给警报。 Or is this even possible?
还是有可能吗?
It seems the problem was that I was posting newline characters to my custom message string as \\n
, but java parsed them as its own newlines, instead of just sending \\n
to java script, which resulted in some form of invalid string that it wouldn't parse. 看来问题在于我将换行符以
\\n
发布到我的自定义消息字符串中,但是java将它们解析为自己的换行符,而不是仅仅将\\n
发送到Java脚本中,这会导致某种形式的无效字符串,不解析。
I fixed the problem by replacing \\n with
\\n` in the custom string sent by the servlet. 我通过在Servlet发送的自定义字符串
\\n with
\\ n`替换\\n with
解决此问题。
And for anyone else in this predicament, this is the syntax that I got it working with: 对于这个困境中的其他人,这是我使用的语法:
<c:if test="${not empty invalidConcertInputMessage}">
<script>
var message = "${invalidConcertInputMessage}";
alert(message);
</script>
</c:if>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.