zoukankan      html  css  js  c++  java
  • Spring Boot结合thymeleaf

    之前在Eclipse里写了个Spring Boot响应jsp的小demo,后来发现打成jar包导出之后找不到jsp文件了。经过在网上查阅信息与资料,发现Spring Boot对于jsp的支持其实是不好的,而且在一些书中和官方都明确表示没有办法支持在jar包中打入jsp文件。虽然也有些朋友发现将Spring Boot的版本降到1.4.2,通过插件可以打进去并且访问到。但其实已经说明了一个问题,也就是既然选用了Spring Boot,就不要再用jsp了。

    讲了那么多,现在来分享一下Spring Boot结合thymeleaf的实例。

    由于在另一篇随笔里已经详述过如何在Eclipse里构建一个Spring Boot工程,这里就不再细说。

    大致的目录结构如下

    pom中的相关依赖如下

    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>  
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>  
      </properties>  
      
      <dependencies>  
        <dependency>  
          <groupId>org.springframework.boot</groupId>  
          <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>  
          <groupId>org.springframework.boot</groupId>  
          <artifactId>spring-boot-starter-thymeleaf</artifactId>  
          </dependency>  
      </dependencies> 

    application.properties中相关配置如下

    server.port=8080  
    server.session.timeout=10  
      
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.content-type=text/html  
    spring.thymeleaf.cache=false

    入口类如下

    package com.thymeleaf;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    ctrl层demo如下

    package com.thymeleaf.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class DemoController {
    
        @RequestMapping("/")
        public String index() {
            return "index";
        }
    }

    然后启动,在浏览器中输入localhost:8080即可跳转到index.html了

  • 相关阅读:
    [leetcode] Valid Sudoku
    [leetcode] Count and Say
    [leetcode] Decode Ways
    [leetcode] Sqrt(x)
    [leetcode] Best Time to Buy and Sell Stock II
    7-27 兔子繁衍问题
    7-26 最大公约数和最小公倍数
    7-25 求奇数和
    7-24 猜数字游戏
    7-23 分段计算居民水费
  • 原文地址:https://www.cnblogs.com/xuzichao/p/8624164.html
Copyright © 2011-2022 走看看