在javascript下使用表达式语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24449784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:16:32  来源:igfitidea点击:

Using Expression language under javascript?

javajavascriptjspel

提问by user3198603

I have below CheckBox in JSP file

我在 JSP 文件中有以下 CheckBox

<input type="checkbox" name="vehicle" value="Bike"
    onclick="javascript:selectCustomers(${sessionScope.custId});">

Getting the following error:

得到以下错误:

org.apache.jasper.JasperException: customer.jsp(1419,33) According to TLD or attribute directive in tag file,
        attribute onclick does not accept any expressions

org.apache.jasper.JasperException: customer.jsp(1419,33) 根据 TLD 或标记文件中的属性指令,
        属性 onclick 不接受任何表达式

Can we not use expression language in JavaScript (in my case under onClick()Event)?

我们不能在 JavaScript 中使用表达式语言(在我的情况下是在onClick()Event下)吗?

采纳答案by Braj

When a JSP page is called, the following happens, in this order:

调用 JSP 页面时,会按以下顺序发生以下情况:

  • Server checks to see if the .jsp has already been compiled and whether or not it has changed since it was last compiled.
  • Server runs the jsp through the Jasper compiler, which interprets the jsp into Java code, anything that is not Java (CSS, HTML, JavaScript, etc) is placed in a String.
  • The Java code is compiled and executed.
  • The results are placed in the response and sent to the user.
  • 服务器会检查 .jsp 是否已经编译,以及自上次编译后它是否已更改。
  • 服务器通过 Jasper 编译器运行 jsp,该编译器将 jsp 解释为 Java 代码,任何不是 Java 的(CSS、HTML、JavaScript 等)都放在一个字符串中。
  • Java 代码被编译和执行。
  • 结果放在响应中并发送给用户。

So, your statement: ${sessionScope.custId}is executed before the the HTML is sent to the user, and the input of selectCustomers()function is already set to before calling it.

因此,您的语句:${sessionScope.custId}在将 HTML 发送给用户之前执行,并且selectCustomers()函数的输入在调用之前已经设置为。



For more info have a look at my another post JSP inside ListItems onclick

有关更多信息,请查看我在 ListItems onclick 中的另一个帖子JSP

How to verify it?

如何验证?

Right click in the browser and look at the view source.

在浏览器中右键单击并查看视图源。



Try below sample code that might help you.

尝试以下可能对您有所帮助的示例代码。

Enclose ${...}inside the single quotes.

${...}在单引号内。

<c:set var="custId" value="1234" scope="session" />

Before :
<c:out value="${sessionScope.custId}"></c:out>

<input type="checkbox" name="vehicle" value="Bike"
    onclick="javascript:selectCustomers('${sessionScope.custId}');">

<c:set var="custId" value="4321" scope="session" />

After:
<c:out value="${sessionScope.custId}"></c:out>

View Source code: (Right click in browser to view it)

查看源代码:(在浏览器中右击查看

Before : 1234

<input type="checkbox" name="vehicle" value="Bike"
    onclick="javascript:selectCustomers('1234');">  

After: 4321

回答by Nikhil Talreja

Try this:

尝试这个:

<input type="hidden" id="custId" name="custId" value="${sessionScope.custId}">

<input type="checkbox" name="vehicle" value="Bike" onclick="javascript:selectCustomers();">
function selectCustomers(){
   var custId = document.getElementById('custId').value;
}