最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jsp - passing a jstl list to a javascript function under onclick event - Stack Overflow

programmeradmin2浏览0评论

as the title says. I got an object gotten from the Controller, on which one of its attributes is a List. Now i want to pass this List to an external file javascript function.

Something like this:

<a href="#" onClick="showAuthorityOverlay(<c:out value='${userDetail.grantedAuthorityList}'/>)">[SHOW AUTHORITY]</a>

Now the value passed to the javascript is something like this:

[ADMIN_USER, COMMON_USER]

So when i click in that link, i get a javascript error saying:

ADMIN_USER isn't defined.

What's wrong here? is it taking the ADMIN_USER and COMMON_USER as variable names? or what? Kinda weird.

Tried even making an inner script in my jsp to get the list like this:

<script type="text/javascript>
    function showAuthorityOverlay() {
          var obj = "<c:out value='${userDetail.grantedAuthorityList}'/>";
          sendToExternalJSFile(obj);
    }
</script>

But still getting the same results. Looks like the values aren't passing correctly through as a List parameter.

as the title says. I got an object gotten from the Controller, on which one of its attributes is a List. Now i want to pass this List to an external file javascript function.

Something like this:

<a href="#" onClick="showAuthorityOverlay(<c:out value='${userDetail.grantedAuthorityList}'/>)">[SHOW AUTHORITY]</a>

Now the value passed to the javascript is something like this:

[ADMIN_USER, COMMON_USER]

So when i click in that link, i get a javascript error saying:

ADMIN_USER isn't defined.

What's wrong here? is it taking the ADMIN_USER and COMMON_USER as variable names? or what? Kinda weird.

Tried even making an inner script in my jsp to get the list like this:

<script type="text/javascript>
    function showAuthorityOverlay() {
          var obj = "<c:out value='${userDetail.grantedAuthorityList}'/>";
          sendToExternalJSFile(obj);
    }
</script>

But still getting the same results. Looks like the values aren't passing correctly through as a List parameter.

Share Improve this question edited Apr 10, 2013 at 15:09 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Apr 10, 2013 at 15:03 msqarmsqar 3,0406 gold badges52 silver badges100 bronze badges 4
  • The generated JavaScript will look like showAuthorityOverlay([ADMIN_USER, COMMON_USER]). Is this right, or maybe should be showAuthorityOverlay('[ADMIN_USER, COMMON_USER]')? – Luiggi Mendoza Commented Apr 10, 2013 at 15:04
  • Yeah but how can I achieve that? Tried but is hard because of the quotes. <a href="#" onClick="<c:out value=\'${userDetail.grantedAuthorityList}\'/>">[SHOW AUTHORITY]</a> – msqar Commented Apr 10, 2013 at 15:17
  • 1 You can do it at server side appending the ' when building the String or using an EL function to add the ' symbol from that input. I would remend changing this at server side. – Luiggi Mendoza Commented Apr 10, 2013 at 15:21
  • Yes, that's what i did now :) Thanks! it worked. – msqar Commented Apr 10, 2013 at 15:24
Add a ment  | 

1 Answer 1

Reset to default 4

JSTL code is executed at server-side. In this case, instead of being used to generate HTML, it's also being used to generate JavaScript code (which is perfectly valid).

The toString() method of your list, which <c:out> calls, returns the toString representation of every Java object in the list, separates them by mas, and surrounds them with brackets. The end result being

[ADMIN_USER, COMMON_USER]

The generate HTML + JavaScript is then downloaded by the browser, which interprets the JavaScript code:

showAuthorityOverlay([ADMIN_USER, COMMON_USER]);

This happens (by accident) to be syntaxically correct JavaScript code. It means: call the function showAuthorityOverlay() with a JavaScript array as argument. The array contains the value of the two JavaScript variables ADMIN_USER and COMMON_USER.

I assume that you in fact want a JavaScript array of strings instead, which should be written as ['ADMIN_USER', 'COMMON_USER'].

What you should do is transform the Java array into a JSON string in the controller, and then use this JSON string inside the JavaScript code:

showAuthorityOverlay(${jsonString})
发布评论

评论列表(0)

  1. 暂无评论