0

我按照这篇文章使用 JSTL 变量呈现 javascript 变量:

var size= "<c:out value='${fn:length(orders)}'/>";

但是,不太幸运,我遇到了这样的异常:

Element type "size" must be followed by either attribute specifications, ">" or "/>".

所以我用

var size= &lt;c:out value='${fn:length(orders)}'/&gt;;

相反,但没有运气。添加双引号后:

var size= "&lt;c:out value='${fn:length(orders)}'/&gt;";

仍然无法正常工作..那么如何逃脱并使其工作?

更新 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>
4

2 回答 2

0

Based on the answer of the link of the post, it would be better if you first define a variable with your needs and then use the value of that variable in JavaScript:

<c:set var="ordersLength" value="${fn:length(orders)}"/>
<script type="text/javascript">
    var size= "<c:out value='${ordersLength}'/>";
</script>

Another option would be setting the value of ${fn:length(orders)} in a hidden field and read this field value in JavaScript:

<input type="hidden" id="ordersLength" value="${fn:length(orders)}" />
<script type="text/javascript">
    var size = document.getElementById('ordersLength').value;
</script>

Of course, this last option is clumsy but might help you to solve this issue.

If none of these work it means your orders variable isn't set nor as request attribute nor as session attribute nor as application attribute nor as page attribute (and by the provided code in your answer, we aren't capable to help you with that [yet]).

于 2013-04-26T21:13:28.260 回答
0

你不能让它与 jspx 文件一起工作。你可以用jsp文件。

jspx 文件需要格式良好的 XML。在处理 EL 之前验证 XML,因此您的代码将失败。

请参阅此帖子以获得更好的解释

于 2015-06-19T12:23:26.777 回答