我有简单的JSP:



<jsp:directive.attribute name="severity" type="java.lang.String" required="true"/>
<jsp:directive.attribute name="currentSeverity" type="java.lang.String" required="true"/>

<c:if test="${severity ne currentSeverity}">
    <c:url value="/session" var="url">
        <c:param name="severity" value="${severity}"/>
    </c:url>
    <li><a href="#" onclick="$.ajax({
        type: 'GET',
        url: '${url}',
        success: function() {
            window.location.reload();
        }
    });"><c:out value="${severity}"/></a></li>
</c:if>




但是,当我评估它时,servlet引擎会抛出:

org.apache.jasper.JasperException:
  /WEB-INF/tags/severity-position.tagx(17,9) PWC6287:
  The attribute prefix success does not correspond to any imported tag library


JSP引擎以某种方式认为$.ajax({...});字符串是JSP EL表达式(无论在${之间放置什么字符)。当我反斜杠{$都没问题时,但是我的IDE认为此代码是损坏的JS代码。

那么,为什么JSP引擎认为$.ajax({...})是JSP EL表达式?

最佳答案

这看起来确实像是所讨论的servlet容器(或Web应用程序)使用的EL实现中的错误。您需要确定它正在使用哪个,然后尝试升级/替换它。

如果升级/替换EL实现无济于事,那么我强烈建议您将整个脚本移到自己的.js文件中的函数中,并使用HTML <script>元素中的<head>加载该文件。这也是正常的做法。更重要的是,我会给链接一个类名,这样您就可以在$(document).ready()期间将其钩住,并相应地添加click()。例如。

<a href="${url}" class="someName">




$(document).ready(function() {
    $('.someName').click(function() {
        $.ajax({
            type: 'GET',
            url: $(this).attr('href'), // This sets the actual value of ${url}.
            success: function() {
                window.location.reload();
            }
        });
        return false; // Blocks link from executing the default href action.
    });
});

07-25 22:46
查看更多