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

javascript - Struts html tag inside <script> - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 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").

发布评论

评论列表(0)

  1. 暂无评论