0

Hello all I have a bean that has 3 getters. In the JSP I use JSTL to iterate over the bean to populate a table. I have saome javascript I need to do the same thing to construct an array. Here it is hardcoded, but how can I contruct it by itearing over a bean?

Bean: This is how I do it in the JSP using JSTL

<c:forEach var="bean" items="${beans}">
     ${bean.month}       
     </c:forEach>

How Can I do the same thing here:

Javascript:

"categories": [{
    "category": [{
       "label": "Oct"
    }, {
        "label": "Nov"
    }, {
        "label": "Dec"
    }, {
       "label": "Jan"
    }, {
        "label": "Feb"
    }, {
        "label": "Mar"
    }, {
        "label": "Apr"
    }, {
        "label": "May"
    }, {
        "label": "Jun"
    }, {
        "label": "Jul"
    }, {
        "label": "Aug"
    }, {
        "label": "Sep"
    }]     
    }]

Trying to do something like this in javascript

 <c:forEach var="bean" items="${beans}">
     [{
       "label": " ${bean.month}"
    },         
     </c:forEach>
6
  • That's not valid JavaScript or JSON, so you'll just get a syntax error. Commented Aug 10, 2012 at 18:49
  • Are you trying to construct an array, or iterate over an existing array? Commented Aug 10, 2012 at 18:52
  • 1
    Your question is really unclear, are you trying to generate JSON in a JSP or work with an object in Javascript? Commented Aug 10, 2012 at 18:53
  • The part I have labeled javascript is what im trying to iterate over and create. Its currently hardcoded. But I have a bean object that has a getter and I want to use that to populate all the months Commented Aug 10, 2012 at 18:56
  • 1
    In that case, since it seems you want to generate JSON in a JSP, I'd recommend taking a look at the json-taglib. It's a set of custom JSP tags for generating JSON data. Not the only set of custom tags to do that, but it is the one I'm familiar with. Commented Aug 10, 2012 at 19:15

2 Answers 2

1

I am not well experienced in JSTL. This is a guess based on the experience I have in PHP.

var array = [
<c:forEach var="bean" items="${beans}" varStatus="beanStatus">
    {
        "label": "${bean.month}"
    }
    <c:if test="${!beanStatus.last}">  // put comma after all item, but last one
         ,
    </c:if>          
 </c:forEach>
];

or

var array = [];
<c:forEach var="bean" items="${beans}">
    array.push({
        "label": "${bean.month}"
    });         
</c:forEach>
Sign up to request clarification or add additional context in comments.

Comments

0
var category = [], // new Array
    i,
    newCategory;

for (i = 0; i < beans.length; i += 1) {
    newCategory = {}; // new object
    newCategory.label = beans[i].month;
    category.push(newCategory);
}

1 Comment

hmmmmmmmmmmmm...trying this also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.