Javascript和scriptlet:可以做到吗?

时间:2012-05-07 17:50:46

标签: java javascript jsp scriptlet

  

可能重复:
  How to use scriptlet inside javascript

<%
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>




<%}%>

是否可以拥有此类代码?还有其他选择吗?

2 个答案:

答案 0 :(得分:0)

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

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

打印到页面时,它将显示如下:

parent.error.document.write("myException");

答案 1 :(得分: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看起来更自然。

相关问题