0

私たちは通常、jsp でこの JSTL のようなことを行います....しかし、JSON を使用して javascript にデータを設定する必要があります。Javascript/JSONでこのようなことをするにはどうすればよいですか

基本的に、Bean をループしてそのパラメーターを引き出します。

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

だから私はこのようなものの中にそれを置くことができます:

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

    }, {

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

2 に答える 2

1

探している JSON 構造は、それぞれに と の 2 つのプロパティを持つオブジェクトの配列のようnameですbreakoutAllowed。このデータを変数に代入する JavaScript は、JSP ファイルからこのように出力できます。<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>
]
于 2012-07-05T19:35:06.187 に答える
1

このコードがうまく機能し、気に入っていただけることを願っています。スクリプトレットをJSに書くのはちょっと変わったやり方だと思うので、それにアプローチする別の方法を提案しました。必要に応じてコメントしてください。

$(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>

     />
于 2012-07-05T20:04:22.180 に答える