首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >百里叶th:inline="javascript“问题

百里叶th:inline="javascript“问题
EN

Stack Overflow用户
提问于 2015-09-16 06:16:35
回答 2查看 56.3K关注 0票数 15

我不知道如何解决以下问题:我想让我的模型基于一些模型逻辑动态生成真正的javascript。

最后一段javascript代码应该添加到我的html页面的$(.ready).ready{}部分中。

问题是:如果我使用inline="javascript",代码会被引用,因为我的getter是一个字符串(这就是在百里叶文档中提到的,但这不是我需要的;-)

如果我使用inline=,“text”in没有引号,但所有引号都转义了;-) -也不错,但不可用8)

如果我尝试inline="none“,什么也不会发生。

下面是一些例子

我的模型getter创建了以下Javascript代码。

PageHelper类

public String documentReady() {

// do some database operations to get the numbers 8,5,3,2
return "PhotoGallery.load(8,5,3,2).loadTheme(name='basic')";

}

所以如果我现在尝试使用inline="javascript"

<script th:inline="javascript">
/*<![CDATA[*/
    jQuery().ready(function(){
        /*[[${pageHelper.documentReady}]]*/
    });
/*]]>*/
</script>

它将被呈现到

<script>
/*<![CDATA[*/
    jQuery().ready(function(){
        'PhotoGallery.load(8,5,3,2).loadTheme(name=\'basic\')'
    });
/*]]>*/
</script>

这没有帮助,因为它是一个字符串文字,仅此而已(这是Thymeleaf处理它的方式)。

因此,如果我尝试使用inline="text"

<script>
/*<![CDATA[*/
    jQuery().ready(function(){
        PhotoGallery.load(8,5,3,2).loadTheme(name=&#39;basic&#39;)
    });
/*]]>*/
</script>

对引号进行转义。

inline="none"我真的不明白,因为它什么也不做

<script>
/*<![CDATA[*/
    jQuery().ready(function(){
        [[${pageHelper.documentReady}]]
    });
/*]]>*/
</script>

老实说,我不知道如何解决这个问题,希望任何人都知道如何处理这个问题。

非常感谢提前干杯约翰

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-18 05:14:51

我会改变方法。

Thymeleaf很容易让你在你的模板中添加要在Javascript中使用的模型变量。在我的实现中,我通常将这些变量放在关闭header标记之前的某个位置;以确保一旦JS加载,它们就会出现在页面上。

当然,我让模板来决定到底要加载什么。如果您正在显示一个库,那么按照您的方式呈现它,并使用数据属性来定义与某些JS代码相关的库。然后给自己写一个不错的jQuery plugin来处理你的图库。

一个比较基本的例子:

默认布局装饰器: layout/default.html

<!doctype html>
<html xmlns:layout="http://www.thymeleaf.org" xmlns:th="http://www.thymeleaf.org">
<head>
  <title>My Example App</title>
  <object th:remove="tag" th:include="fragments/scripts :: header" />
</head>
<body>
  <div layout:fragment="content"></div>
  <div th:remove="tag" th:replace="fragments/scripts :: footer"></div>
  <div th:remove="tag" layout:fragment="footer-scripts"></div>
</body>
</html>

这里需要注意的是包含了通用的脚注脚本,然后是定义的layout:fragment div。我们将使用这个布局div来包含图库所需的jQuery插件。

包含常规脚本的文件: fragments/scripts.html

<div th:fragment="header" xmlns:th="http://www.thymeleaf.org">
  <script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var MY_APP = {
      contextPath: /*[[@{/}]]*/,
      defaultTheme: /*[[${theme == null} ? null : ${theme}]]*/,
      gallery: {
        theme: /*[[${gallery == null} ? null : ${gallery.theme}]]*/,
        images: /*[[${gallery == null} ? null : ${gallery.images}]]*/,
        names: /*[[${gallery == null} ? null : ${gallery.names}]]*/
      }
    };
    /*]]>*/
  </script>
</div>
<div th:fragment="footer" xmlns:th="http://www.thymeleaf.org">
  <script type="text/javascript" src="/js/jquery.js"></script>
  <script type="text/javascript" src="/js/my_app.js"></script>
</div>

在脚本文件中,有2个片段,它们来自装饰器。在头片段中,为JS层包含了一个有用的上下文路径,还包含了一个用于地狱的defaultTheme。然后从我们的模型中定义并分配一个图库对象。脚注片段加载jQuery库和主站点JS文件,同样出于本例的目的。

包含惰性加载的库的页面: products.html

<html layout:decorator="layout/default" xmlns:layout="http://www.thymeleaf.org/" xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Products Landing Page</title>
</head>
<body>
  <div layout:fragment="content">
    <h1>Products</h1>
    <div data-gallery="lazyload"></div>
  </div>
  <div th:remove="tag" layout:fragment="footer-scripts">
    <script type="text/javascript" src="/js/my_gallery.js"></script>
  </div>
</body>
</html>

我们的产品页面上没有太多内容。使用默认的装饰器,此页面将覆盖页眉中的页面标题。我们的内容片段包含一个h1标记中的标题和一个具有数据库属性的空div。这个属性是我们将在jQuery插件中用来初始化库的属性。

该值被设置为lazyload,因此我们的插件知道我们需要在某个变量集中查找图像in。如果我们的插件唯一支持的是一个延迟加载的库,那么这个库很容易就是空的。

因此,布局加载了一些默认脚本,通过巧妙地放置layout:fragments,您可以允许站点的某些部分独立于其他部分加载库。

下面是一个基本的Spring控制器示例,用于我们的应用程序: MyController.java

@Controller
public class MyController {
  @RequestMapping("/products")
  public String products(Model model) {        
    class Gallery {
      public String theme;
      public int[] images;
      public String[] names;
      public Gallery() {
        this.theme = "basic";
        this.images = new int[] {8,5,3,2};
        this.names = new String[] {"Hey", "\"there's\"", "foo", "bar"};
      }
    }
    model.addAttribute("gallery", new Gallery());
    return "products";
  }
}

为了简化我们的示例,在products方法中内联了Gallery类。这可以很容易地成为返回标识符数组的某种类型的服务或存储库,或者您需要的任何东西。

我们创建的jQuery插件可能如下所示: my_gallery.js

(function($) {
  var MyGallery = function(element) {
    this.$el = $(element);
    this.type = this.$el.data('gallery');
    if (this.type == 'lazyload') {
      this.initLazyLoadedGallery();
    }
  };

  MyGallery.prototype.initLazyLoadedGallery = function() {
    // do some gallery loading magic here
    // check the variables we loaded in our header
    if (MY_APP.gallery.images.length) {
      // we have images... sweet! let's fetch them and then do something cool.
      PhotoGallery.load(MY_APP.gallery.images).loadTheme({
        name: MY_APP.gallery.theme
      });
      // or if load() requires separate params
      var imgs = MY_APP.gallery.images;
      PhotoGallery.load(imgs[0],imgs[1],imgs[2],imgs[3]).loadTheme({
        name: MY_APP.gallery.theme
      });
    }
  };

  // the plugin definition
  $.fn.myGallery = function() {
    return this.each(function() {
      if (!$.data(this, 'myGallery')) {
        $.data(this, 'myGallery', new MyGallery(this));
      }
    });
  };

  // initialize our gallery on all elements that have that data-gallery attribute
  $('[data-gallery]').myGallery();
}(jQuery));

products页面的最终呈现将如下所示:

<!doctype html>
<html>
<head>
  <title>Products Landing Page</title>
  <script type="text/javascript">
    /*<![CDATA[*/
    var MY_APP = {
      contextPath: '/',
      defaultTheme: null,
      gallery: {
        theme: 'basic',
        images: [8,5,3,2],
        names: ['Hey','\"there\'s\"','foo','bar']
      }
    };
    /*]]>*/
  </script>
</head>
<body>
  <div>
    <h1>Products</h1>
    <div data-gallery="lazyload"></div>
  </div>
  <script type="text/javascript" src="/js/jquery.js"></script>
  <script type="text/javascript" src="/js/my_app.js"></script>
  <script type="text/javascript" src="/js/my_gallery.js"></script>
</body>
</html>

正如你所看到的,Thymeleaf在将你的模型转换成有效的JS方面做得非常好,实际上在需要的地方添加了引号,并对它们进行了转义。一旦页面完成渲染,jQuery插件在文件的末尾,初始化图库所需的一切都应该加载并准备就绪。

这不是一个完美的例子,但我认为这是一个非常简单的web应用程序设计模式。

票数 11
EN

Stack Overflow用户

发布于 2018-10-25 06:05:16

使用${pageHelper.documentReady}代替${pageHelper.documentReady}

票数 -4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32596600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档