English 中文(简体)
jsp 中包含的多标称标记
原标题:Multiple Javascript tag inclusions in jsp

这是我的代码在jsp:

<script>  
var myArray = [];
</script>
    <c:forEach var="attributes" items="FROMthisBEAN"  varStatus="vStatus" >  
                <script>
                 //Executes for each iteration. Do something COOL.
                 myArray.push("Something from this iteration");
                </script>
    </c:forEach>

现在请考虑以下各点:

<script>  
    var myArray = [];    
        <c:forEach var="attributes" items="FROMthisBEAN"  varStatus="vStatus" >  
                    myArray.push("Something from this iteration");
        </c:forEach>
 </script>
<c:forEach var="attributes" items="FROMthisBEAN"  varStatus="vStatus" >  
                //Executes for each iteration. Do something COOL.
</c:forEach>

Both the codes give me the same output.
The question is which one is better when it comes to performance?
In the first case the script tag inside c:forEach repeats again and again.
But in the second case I m creating one more c:forEach which is already present in the JSP.
Totally lost here. Please advise.

最佳回答

完全没有必要将这些东西放入单独的 标签中。 如果您真的想把它做得更好, 您可能应该考虑创建 JavaScript 阵列文字, 而不是一系列的“ push () ” 调用 。 JSP 扩展将 Java 阵列变成 JSON 的游戏 。

edit - 详细描述,作为 JSTL 函数提供的 JSON 编码器将允许您写出类似的东西 :

<script>
  var myArray = ${yourTLD:toJSON( some.java.array _)};
</script>

“ toJSON” 函数将把阵列作为标准 JSON, 视其内容而定( 当然, 当然, 根据使用的 JSON 代码, 可能有些限制 ) 。 发送到浏览器的 JavaScript 将看起来像 :

<script>
  var myArray = [ "something", "something", "something" ];
</script>

完全取决于 Java 阵列中的内容。 JSON 为 Java 设置了各种编码库, 写一个库并不难( 事实上, 在特殊情况下, 它可以是 < em> easier , 比对打开源库进行修改要难得多 ) 。 提供该功能作为 JSTL EL 函数的问题在于在某处创建公共静态函数, 并在您的. tld 文件中声明 。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签