この投稿に従って、 JSTL 変数を使用して JavaScript 変数をレンダリングします。
var size= "<c:out value='${fn:length(orders)}'/>";
ただし、幸運ではありませんが、次のような例外が発生します。
Element type "size" must be followed by either attribute specifications, ">" or "/>".
だから私は使う
var size= <c:out value='${fn:length(orders)}'/>;
代わりに、しかし運はありません。二重引用符を追加した後:
var size= "<c:out value='${fn:length(orders)}'/>";
まだ機能していません..では、エスケープして機能させる方法は?
更新 1
jspx は次のようになります。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jsp:root
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:page="urn:jsptagdir:/WEB-INF/tags/form"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
version="2.1">
<div>
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<page:show>
<form:form.....
<!--body of the form -->
</form:form>
</page:show>
<script type="text/javascript">
//jquery and so...
....
var size= "<c:out value='${fn:length(orders)}'/>";
....
</script>
</div>
</jsp:root>