在javascript下使用表达式语言

Using Expression language under javascript?

本文关键字:表达式 语言 javascript      更新时间:2023-09-26

我在JSP文件中有下面的CheckBox

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

出现以下错误:

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

我们可以不使用表达式语言在JavaScript(在我的情况下onClick()事件)?

当调用JSP页面时,按此顺序发生以下操作:

  • 服务器检查.jsp是否已经编译,以及自上次编译以来是否发生了更改。
  • 服务器通过Jasper编译器运行jsp,该编译器将jsp解释为Java代码,任何非Java的内容(CSS、HTML、JavaScript等)都放在String中。
  • Java代码被编译并执行
  • 将结果放在响应中并发送给用户。

所以,你的语句:${sessionScope.custId}在HTML发送给用户之前执行,selectCustomers()函数的输入在调用它之前已经设置为。


有关更多信息,请查看我的另一篇文章JSP inside listtitems onclick

如何验证?

在浏览器中右键单击,查看视图源代码。


试试下面的示例代码,可能会对你有所帮助。

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

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

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

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

试试这个:

<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;
}