简体   繁体   English

在Javascript中迭代bean?

[英]Iterate over bean in Javascript?

We normally do something like this JSTL on the jsp....but I now need to set data in the javascript using JSON. 我们通常在jsp上做这样的JSTL ......但我现在需要使用JSON在javascript中设置数据。 How would I do something like this in Javascript/JSON 我如何在Javascript / JSON中做这样的事情

Basically loop over a bean and pull out its parameters: 基本上循环遍历bean并提取其参数:

  <c:forEach var="field" items="${detFields}">
      ${field.groupName}
          ${field.breakoutAllowed}           
 </c:forEach>

so I can put it inside something like this: 所以我可以把它放在这样的东西:

    "data": [{
        "value": "${field.groupName}"

    }, {

        "value": "${field.breakoutAllowed}"  
    }]

It looks like the JSON structure you're looking for is an array of objects, each with two properties: name and breakoutAllowed . 看起来你正在寻找的JSON结构是一个对象数组,每个对象都有两个属性: namebreakoutAllowed JavaScript to assign this data to a variable could be output like this from a JSP file. 将此数据分配给变量的JavaScript可以从JSP文件中输出。 (Within a <script> block or equivalent, of course.) (当然,在<script>块或等效内容中。)

var output = [
 <c:forEach var="field" items="${detFields}" varStatus="status">
   { 
     "name":"${field.groupName}",
     "breakoutAllowed":"${field.breakoutAllowed}" 
   }
   <c:if test="${not status.last}">,</c:if>
 </c:forEach>
]

Hope this code works well and is liked by you. 希望这段代码运行良好,并且很受你喜欢。 I consider writing scriptlet into JS a kinda weird practice so just suggested another way of approaching it. 我认为将scriptlet编写成JS是一种奇怪的做法,所以只是提出了另一种方法来处理它。 Please comment if necessary. 如有必要请发表评论。

$(document).ready(function(){
    var k=$('#data').val();

    //Convert the value into the required array. Since writing it is JavaScript directly is not a daily practice.
    })

    <input type="hidden" id="data" value=
     <c:forEach var="field" items="${detFields}" varStatus="status">
       { 
         "name":"${field.groupName}",
         "breakoutAllowed":"${field.breakoutAllowed}" 
       }
       <c:if test="${not status.last}">,</c:if>
     </c:forEach>

     />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM