سؤال

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

هل كانت مفيدة؟

المحلول

To 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 comma, 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").

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top