I know that this is a very noob and dumb question, but I need help. Tried several topics and none's working.
So I'm trying to pass a List created in Struts2(java) into javascript to draw a chart using highlight. I've read several articles and e up with this:
$(function drawList() {
var list = [
<c:forEach items="${listFromJava}" var="alistFromJava">
{itemName: "${alistFromJava.attribute}"},
</c:forEach>
];
However it never works, and always ends up with: Static attribute must be a String literal, its illegal to specify an expression.
If I try:
list = '<s:property value="listFromJava"/>
then it returns the reference only.
Any suggestion is appreciated. Thanks in advance.
I know that this is a very noob and dumb question, but I need help. Tried several topics and none's working.
So I'm trying to pass a List created in Struts2(java) into javascript to draw a chart using highlight. I've read several articles and e up with this:
$(function drawList() {
var list = [
<c:forEach items="${listFromJava}" var="alistFromJava">
{itemName: "${alistFromJava.attribute}"},
</c:forEach>
];
However it never works, and always ends up with: Static attribute must be a String literal, its illegal to specify an expression.
If I try:
list = '<s:property value="listFromJava"/>
then it returns the reference only.
Any suggestion is appreciated. Thanks in advance.
Share Improve this question edited Oct 2, 2013 at 9:46 Andrea Ligios 50.3k29 gold badges124 silver badges248 bronze badges asked Oct 1, 2013 at 3:34 user1509803user1509803 1961 silver badge9 bronze badges1 Answer
Reset to default 6To avoid confusion while googling:
<c:forEach
is JSTL
${listFromJava}
is EL
<s:property
is STRUTS2 UI TAG
listFromJava
(or %{listFromJava}
") is OGNL
The Struts2 Tag that replaces JSTL's forEach
is <s:iterator>
.
Your function may be rewritten in pure Struts2 like this:
$(function drawList() {
var list = [
<s:iterator value="listFromJava" >
{itemName: '<s:property escapeJavascript="true" value="attribute"/>'},
</s:iterator>
];
});
To prevent the last element to have an undesidered ma, use <s:if>
$(function drawList() {
var list = [
<s:iterator value="listFromJava" status="stat">
<s:if test="#stat.index>0">,</s:if>
{itemName: '<s:property escapeJavascript="true" value="attribute"/>'}
</s:iterator>
];
});
EDIT: added the escaping needed to prevent javascript injection issues (escapeJavascript="true"
).