0

可能重复:
如何在 javascript 中使用 scriptlet

<%
if(request.getAttribute("exception")!=null){%>

<script type="text/javascript" >
    alert("hi");

    parent.error.document.open();
    parent.error.document.write("bye");
    parent.error.document.write(<%=request.getAttribute("exception").toString()%>);
    parent.error.document.close();

</script>
</form>




<%}%>

有可能有这种代码吗?有没有其他选择?

4

2 回答 2

0

您的代码应该可以工作。但我更喜欢 JSTL:

<c:if test="${not empty(exception)}">
    <script type="text/javascript">
        alert("hi");
        parent.error.document.open();
        parent.error.document.write("bye");
        parent.error.document.write(<%=request.getAttribute("exception").toString()%>);
        parent.error.document.close();

    </script>
</c:if>

对于jsp看起来更自然。

于 2012-05-08T07:20:55.853 回答
0

您只是缺少一些引号来将值视为字符串,而不是变量。

parent.error.document.write("<%=request.getAttribute("exception").toString()%>");

打印到页面时会如下所示:

parent.error.document.write("myException");
于 2012-05-07T18:03:14.943 回答