Thymeleaf教程 (二) 虚拟购物商店

示例所示的源代码和本指南中未来的章节都可以在此找到Good Thymes Virtual Grocery GitHub repository.

一个购物网站

为了更好地解释Thymeleaf参与处理模板的概念,本教程将使用一个演示的应用程序,您可以从上面地址下载项目。  

这个应用程序是一个虚构的虚拟商店网站,并将为我们提供足够的场景举例说明不同Thymeleaf特性。

我们需要为我们的应用程序设计一套非常简单的实体模型: Products(商品) 通过创建orders(订单)卖给Customers(客户)。我们也管理这些产品的Comments(评论)。应用程序模型如下: 

我们的小应用程序也将有一个非常简单的服务层,服务对象包含方法如下:

public class ProductService {
  ...
  public List<Product> findAll() {
    return ProductRepository.getInstance().findAll();
  }
  public Product findById(Integer id) {
    return ProductRepository.getInstance().findById(id);
  }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

最后,在web层有一个过滤器,将根据请求URL委托执行Thymeleaf-enabled命令:

private boolean process(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
  try {
    /*
    * Query controller/URL mapping and obtain the controller
    * that will process the request. If no controller is available,
    * return false and let other filters/servlets process the request.
    */
    IGTVGController controller = GTVGApplication.resolveControllerForRequest(request);
    if (controller == null) {
      return false;
    }
    /*
    * Obtain the TemplateEngine instance.
    */
    TemplateEngine templateEngine = GTVGApplication.getTemplateEngine();
    /*
    * Write the response headers
    */
    response.setContentType("text/html;charset=UTF-8");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    /*
    * Execute the controller and process view template,
    * writing the results to the response writer.
    */
    controller.process(request, response, this.servletContext, templateEngine);
    return true;
  } catch (Exception e) {
    throw new ServletException(e);
  }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

这是我们的 IGTVGController 接口:

public interface IGTVGController {
  public void process(HttpServletRequest request, HttpServletResponse response,ServletContext servletContext, TemplateEngine templateEngine);
}
   
   
  • 1
  • 2
  • 3

现在我们要做的就是创建IGTVGController接口的实现,从服务层获取数据并使用TemplateEngine对象处理模板。最后,它会像这样: 

但首先让我们看看模板引擎是如何初始化的。

建立和配置模板引擎

在我们的过滤器的process(…)方法包含这个句子:

TemplateEngine templateEngine = GTVGApplication.getTemplateEngine();
   
   
  • 1

这意味着GTVGApplication类负责创建和配置程序中最重要的一个对象TemplateEngine实例。我们的org.thymeleaf.TemplateEngine对象初始化过程如下:

public class GTVGApplication {

    ...
    private TemplateEngine templateEngine;
    ...
    public GTVGApplication(final ServletContext servletContext) {
        super();
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);

        // HTML is the default mode, but we will set it anyway for better understanding of code
        templateResolver.setTemplateMode(TemplateMode.HTML);
        // This will convert "home" to "/WEB-INF/templates/home.html"
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        // Set template cache TTL to 1 hour. If not set, entries would live in cache until expelled by LRU
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));

        // Cache is set to true by default. Set to false if you want templates to
        // be automatically updated when modified.
        templateResolver.setCacheable(true);

        this.templateEngine = new TemplateEngine();
        this.templateEngine.setTemplateResolver(templateResolver);

        this.controllersByURL = new HashMap<String, IGTVGController>();
        this.controllersByURL.put("/", new HomeController());
        this.controllersByURL.put("/product/list", new ProductListController());
        this.controllersByURL.put("/product/comments", new ProductCommentsController());
        this.controllersByURL.put("/order/list", new OrderListController());
        this.controllersByURL.put("/order/details", new OrderDetailsController());
        this.controllersByURL.put("/subscribe", new SubscribeController());
        this.controllersByURL.put("/userprofile", new UserProfileController());

    }
    ...

}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

当然有很多方法配置TemplateEngine对象,但是现在这些几行代码将满足我们所需。

模板解析器

让我们先从模板解析器开始:

ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
   
   
  • 1

模板解析器是实现了Thymeleaf API中的org.thymeleaf.templateresolver.ITemplateResolver接口的对象。

public interface ITemplateResolver {
  ...
  /*
  * Templates are resolved by String name (templateProcessingParameters.getTemplateName())
  * Will return null if template cannot be handled by this template resolver.
  */
  public TemplateResolution resolveTemplate(
    TemplateProcessingParameters templateProcessingParameters);
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

模板解析器负责确定如何访问我们的模板。并且我们能从servlet上下文环境中解析模板文件。

但上述并不是模板解析器的所有功能,因为我们可以设置一些配置参数。首先, 设置模板模式,一个标准的例子如下:

templateResolver.setTemplateMode("XHTML");
   
   
  • 1

XHTML是ServletContextTemplateResolver默认的模板模式。

templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
   
   
  • 1
  • 2

这些前缀和后缀用来修饰模板引擎调用的模板路径,比如模板的调用模板的路径为“product/list“相当于如下操作

servletContext.getResourceAsStream("/WEB-INF/templates/product/list.html")
   
   
  • 1

可选地,在缓存中解析模板的有效时间是配置的,模板解析器通过cacheTTLMs属性设置

templateResolver.setCacheTTLMs(3600000L);
   
   
  • 1

当然,一个模板在到达最大缓存时间前是可以被去掉的,而到达最大缓存时间的时候,会去掉缓存中最老的。

缓存行为和大小可以由用户通过实现ICacheManager接口或简单修改StandardCacheManager来改变默认缓存对象集。

模板引擎

模板引擎是类org.thymeleaf.TemplateEngine的对象。下面的例子创建了我们的模板引擎:

templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
   
   
  • 1
  • 2

非常简单,不是吗?我们所需要仅仅的是创建一个实例并设置模板解析器给它。  

模板解析器是TemplateEngine唯一需要的参数,当然接下来还有很多其他参数需要设置(消息解析器、缓存大小等)。就目前而言,这就是我们所需要的。  

我们的模板引擎现在准备好了,我们可以开始使用Thymeleaf创建我们的页面了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值