zoukankan      html  css  js  c++  java
  • springBoot---thymeleaf渲染web页面

    1.首先解决一下pom.xml中的这个报错

    报错原因:因为是直接从网上拷贝过来粘贴在pom.xml中的可能格式会有冲突

    把前面的空格都删了,重新排版就行

     

     2.然后把我们的thymeleaf依赖导入进来

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

     出现这些jar即代表导入成功了

    3.我们现在开始编写配置文件

     spring: 

    thymeleaf:
    #指定模板所在的路径
    prefix: classpath:/templates/
    #check-tempate-location:检查模板路径是否存在
    check-tempate-location: true
    #cache:是否缓存,开发模式下设置为false,避免改了模板之后还需要重启服务,线上设置为true,可以提高性能
    cache: false #关闭缓存
    mode: HTML5 #设置模板类型
    encoding: utf-8 #设置编码

    package demo.entity;

    public class myUser {
    private String userName;
    private Integer age;

    public myUser(String userName, Integer age) {
    this.userName = userName;
    this.age = age;
    }

    public void setUserName(String userName) {
    this.userName = userName;
    }
    public void setAge(Integer age) {
    this.age = age;
    }
    public String getUserName() {
    return userName;
    }
    public Integer getAge() {
    return age;
    }

    }

    package demo.controller;

    import demo.entity.myUser;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    import javax.servlet.http.HttpServletRequest;

    @Controller
    public class MyThymeleafController {
    @RequestMapping("/myThymeleaf")
    public String myThymeleaf(HttpServletRequest request){
    request.setAttribute("user",new myUser("xiaotaozi",27));
    return "myThymeleaf";
    }
    }

     启动我们的接口

    本地直接访问




  • 相关阅读:
    python实现单链表及常用方法->判空|长度|头尾及指定插入|删除|搜索|反转
    k8s v1.21.9集群安装带认证的elk 6.8.18
    docker安装Elasticsearch6.8集群并设置密码
    kubernetes v1.20.9部署keepalived-vip实现高可用pod访问
    rsync远程同步工具的使用-实现跳板机与内网数据传输
    旧金山大学 大卫 算法演示
    IO多路复用之select、poll、epoll详解-转载
    docker容器中使用systemctl命令 set-hostname
    win7 安装ss
    常用相似度语料
  • 原文地址:https://www.cnblogs.com/wendy-0901/p/14241599.html
Copyright © 2011-2022 走看看