简体   繁体   English

在JSP标记中使用JavaScript

[英]Using JavaScript within a JSP tag

I've seen this question regading the importing of js-files related to the tag content itself. 我已经看到了这个问题,即重新导入与标签内容本身相关的js文件。 I have a similar problem, here I have a jsp tag that generates some HTML and has a generic js-implementation that handles the behavior of this HTML. 我有一个类似的问题,这里我有一个生成一些HTML的jsp标签,并有一个通用的js实现来处理这个HTML的行为。 Furthermore I need to write some initialization statements, so I can use it afterwards through JavaScript. 此外,我需要编写一些初始化语句,因此我可以通过JavaScript使用它。 To be possible to use this "handler" within my JavaScript, it should be somehow accessible. 为了能够在我的JavaScript中使用这个“处理程序”,它应该以某种方式可访问。

The question is... Is it Ok to write inline <script> tags along with my HTML for instantiation and initialization purposes (personally I don't think its very elegant)? 问题是......是否可以将内联<script>标签与我的HTML一起用于实例化和初始化目的(我个人认为它不是很优雅)? And about being accessible to the JS world, should I leave a global var referencing my handler object (not very elegant aswell I think), are there better ways to do it? 关于JS世界的可访问性,我是否应该保留一个全局变量引用我的处理程序对象(我认为不是很优雅),有更好的方法吗?

You should strive for javascript in its own files. 你应该在自己的文件中争取使用javascript。 This is usually done with Progressive Enhancement . 这通常通过渐进增强来完成。 But some times you don't have a choice, for instance when the same JSP renders pages in different languages. 但有些时候你没有选择,例如当同一个JSP呈现不同语言的页面时。 Here's a real-life example: 这是一个真实的例子:

The JSP: JSP:

  <script src="/javascript/article_admin.js"></script>  
  <script type="text/javascript">  
      NP_ArticleAdmin.initialize({  
            text: {  
              please_confirm_deletion_of: '<i18n:output text="please.confirm.deletion.of"/>',  
              this_cannot_be_undone: '<i18n:output text="this.cannot.be.undone"/>'  
            }  
      });  
  </script>  

The javascript (article_admin.js): javascript(article_admin.js):

 /*global NP_ArticleAdmin, jQuery, confirm */  
 NP_ArticleAdmin = function ($) {  
     var text;  

     function delete_article(event) {  
         var article = $(this).parents("li.article"),  
         id = article.attr("id"),  
         name = article.find("h3.name").html();  
         if (confirm(text.please_confirm_deletion_of + name + text.this_cannot_be_undone)) {  
             $.post("/admin/delete_article", {id: id});  
             article.fadeOut();  
         }  
         event.preventDefault();  
         return false;  
     }  

     function initialize(data) {  
         text = data.text;  
         $("#articles a.delete").click(delete_article);  
     }  

     return {initialize: initialize};  
 }(jQuery);

In this example, the only javascript in the JSP-file is the part that needs to be there. 在此示例中,JSP文件中唯一的javascript是需要存在的部分。 The core functionality is separated in its own js-file. 核心功能在其自己的js文件中分开。

I'm not entirely sure what you asking here, but I don't there's anything wrong with including <script> tags in the JSP to instantiate javascript code. 我不完全确定你在这里问什么,但是我在JSP中包含<script>标签来实例化javascript代码并没有任何问题。 I often follow this model, writing the library code in external javascript files, and then calling the constructors to my objects from the <script> tags. 我经常遵循这个模型,在外部javascript文件中编写库代码,然后从<script>标签调用构造函数到我的对象。

This makes debugging easy, since the logic is all in the external files (and firebug seems to have trouble with debugging inline javascript code). 这使调试变得容易,因为逻辑全部在外部文件中(并且firebug似乎在调试内联javascript代码时遇到问题)。 The libraries get cached, but the data instantiating them doesn't (which is the desired behavior). 库被缓存,但实例化它们的数据却没有(这是所需的行为)。

The alternative is to have the instantiation code dynamically generated in an external javascript file or AJAX call. 另一种方法是在外部javascript文件或AJAX调用中动态生成实例化代码。 I've done this too, with positive results. 我也做到了这一点,取得了积极的成果。

I think the deciding factor is how much dynamic data you have. 我认为决定因素是你拥有多少动态数据。 If you need to represent large data structures, I would serve it out via an AJAX call that returns JSON. 如果您需要表示大型数据结构,我会通过返回JSON的AJAX调用来提供它。 If its a simple call to a constructor, put it in the JSP. 如果它是对构造函数的简单调用,则将其放入JSP中。

As for the global variable, I will often have a global for the top-level object that kicks everything off. 至于全局变量,我经常会有一个全局的顶级对象,可以解决所有问题。 Inside that, are all the other references to the helper objects. 在其中,是对辅助对象的所有其他引用。

Although I agree that it's not entirely elegant, I've been known to do it a few times when combining server-side decisions with an AJAX-integrated environment. 虽然我同意它并不完全优雅,但我已经知道在将服务器端决策与AJAX集成环境相结合时会做几次。 Echoing inline <script> tags in order to initialize some variables isn't a terrible thing, as long as no one sees it. 只要没有人看到它,回应内联<script>标签以初始化一些变量并不是一件可怕的事情。

As for better methods, I am unaware of these. 至于更好的方法,我不知道这些。 I've done this so rarely that I haven't sought a more elegant or "proper" solution. 我很少这样做,所以我没有寻求更优雅或“适当”的解决方案。

It is ok with use <script> tags in line with HTML. 可以使用符合HTML的<script>标签。 There are times when it is needed, but as far as any better ways I do not know. 有时需要它,但就我认识的任何更好的方法而言。 Without making things seem more complicated it is easier to use the <script> tag then trying to find a way to implement js files. 在不使事情变得更复杂的情况下,使用<script>标签然后尝试找到实现js文件的方法更容易。

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

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