【问题标题】:Can I mix JSTL with JavaScript like this?我可以像这样将 JSTL 与 JavaScript 混合吗?
【发布时间】:2014-03-06 12:52:07
【问题描述】:

我可以像这样一起使用 JavaScript 和 JSTL:

<script type="text/javascript">

function selectDifferentAccount(select, id){

    if (id == "0"){
        var balance = "sourceBalance";
        <c:set var="sourceBalance" value="${select}" scope="session" /> 
    }
    else if (id == "1"){
        var balance = "targetBalance";
        <c:set var="targetBalance" value="${select}" scope="session" /> 
        document.write(${targetBalance});
    }
}
</script>

我希望将 select 参数发送到 JSTL 使用的 sessionScope 变量,但不确定这是否可行,因为 document.write 不会打印出任何内容。

【问题讨论】:

    标签: javascript jsp jstl


    【解决方案1】:

    除非变量在范围内,否则不能使用 ${} EL 表达式。

    首先生成服务器端代码,然后生成客户端。

    所以现在控制权来到了 Javascript 方法。 您可以设置该值,因为两者都是不同的环境。

    function selectDifferentAccount(select, id){
    
     if (id == "0"){
        var balance = ${someVariable}; // fine
        <c:set var="sourceBalance" value="${select}" scope="session" />// but not reverse 
     }
    

    【讨论】:

      猜你喜欢
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2012-05-13
      相关资源
      最近更新 更多