将foreach JSP变量作为参数传递给javascript函数

时间:2021-12-17 11:49:16

I have the following code sample which was written in the JSP. But it is not working. I have tried that code snippet from the following links. But it didn't help me to solve the problem.

我有以下代码示例,它是在JSP中编写的。但它没有用。我已尝试过以下链接中的代码段。但它没有帮助我解决问题。

Pass a JSP variable as parameter to javascript function

将JSP变量作为参数传递给javascript函数

Passing Jsp variable to javascript as a function parameter

将Jsp变量作为函数参数传递给javascript

< c:forEach items="${items}" var="item">

   <div onclick="myOnClick('<%=item%>')">${item.title}</div>

< /c>

function myOnClick(item){
    console.log(JSON.stringify(item));
}

Actually I am getting an error in the eclipse that "item cannot be resolved to a variable" for the JSP code.

实际上我在日食中遇到一个错误,即“项目无法解析为变量”,因为JSP代码。

I tried the below one code

我尝试了以下一个代码

      <div onclick="myOnClick(${item})">${item.title}</div>

It was throwing "Syntax error:illegal character" error.

它抛出了“语法错误:非法字符”错误。

Need help to fix this issue. Thank you.

需要帮助来解决此问题。谢谢。

1 个解决方案

#1


0  

I found the answer by my own from the following link.

我通过以下链接找到了答案。

How to access a java object in javascript from JSP?

如何从JSP访问javascript中的java对象?

That is what exactly I was doing in the code. I am getting the Java objects and I am mapping them to the HTML. Once they mapped I wan unable to access them from javascript. But in the above link they have provided the solution to the problem. So we have to call a web service to get the actual object. Thank you @LeleDumbo for providing the answer.

这正是我在代码中所做的。我正在获取Java对象,我将它们映射到HTML。一旦他们映射我无法从javascript访问它们。但在上述链接中,他们提供了解决问题的方法。所以我们必须调用Web服务来获取实际对象。谢谢@LeleDumbo提供答案。

#1


0  

I found the answer by my own from the following link.

我通过以下链接找到了答案。

How to access a java object in javascript from JSP?

如何从JSP访问javascript中的java对象?

That is what exactly I was doing in the code. I am getting the Java objects and I am mapping them to the HTML. Once they mapped I wan unable to access them from javascript. But in the above link they have provided the solution to the problem. So we have to call a web service to get the actual object. Thank you @LeleDumbo for providing the answer.

这正是我在代码中所做的。我正在获取Java对象,我将它们映射到HTML。一旦他们映射我无法从javascript访问它们。但在上述链接中,他们提供了解决问题的方法。所以我们必须调用Web服务来获取实际对象。谢谢@LeleDumbo提供答案。