admin管理员组文章数量:1025790
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 beshowAuthorityOverlay('[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 theString
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
1 Answer
Reset to default 4JSTL 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})
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 beshowAuthorityOverlay('[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 theString
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
1 Answer
Reset to default 4JSTL 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})
本文标签: jsppassing a jstl list to a javascript function under onclick eventStack Overflow
版权声明:本文标题:jsp - passing a jstl list to a javascript function under onclick event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745639432a2160652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论