Spring Boot推荐使用thymeleaf模板完成与页面的交互(已不支持JSP某些特性,不推荐JSP)

步骤

在一个Spring Boot Web项目基础上,也可以参考我前一篇文章建立的项目

1、pom.xml添加thymeleaf依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

<groupId>com.zit</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.BUILD-SNAPSHOT</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   
    <!-- Spring data jpa -->
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  
  <!-- thymeleaf模板 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
 
</dependencies>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

</project>

2、src/main/resources是默认的静态资源目录,在他下面新建一个文件夹templates

他是Spring Boot约定默认的页面路径

在他下面创建hello.html页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
你真好
<h1 th:text="${msg}"></h1>
</html>

3、application.properties

在其中关闭thymeleaf缓存

#DB Configuration:spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password =

#JPA Configuration: 
spring.jpa.database=MySQL
spring.jpa.show-sql=true 
spring.jpa.generate-ddl=true 
spring.jpa.hibernate.ddl-auto=update 
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

#关闭thymeleaf缓存
spring.thymeleaf.cache=false

4、控制器Controller类中:

注意:与web页面交互,用@Controller

package com.zit;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zit.dao.UserLoginDao;
import com.zit.model.UserLogin;

@Controller
@SpringBootApplication
@EnableAutoConfiguration
public class UserLoginController {

@Resource
    UserLoginDao userLoginDAO;

@RequestMapping("/list")
    public List<UserLogin> list(){
     return (List<UserLogin>) userLoginDAO.findAll();
    }
   
    @RequestMapping("/test")
    public String index(ModelMap model){
        model.addAttribute("msg","中华人民共和国");
     return "hello";

}
   
    public static void main(String[] args) {
  SpringApplication.run(UserLoginController.class, args);
 }
}

访问:http://localhost:8080/test

即可跳转到hello.html页面

如果访问:http://localhost:8080/list/

它在页面上返回的是Json数据,Rest风格

所以在类上面的@Controller改为@RestController即可

转载于:https://www.cnblogs.com/Donnnnnn/p/8588570.html

Spring Boot + thymeleaf 后台与页面(二)相关推荐

  1. Spring boot+Thymeleaf+easyui集成:js创建组件页面报错

    开发工具:Ideal 使用场景:Demo 前提:        环境:Spring boot +Thymeleaf+easyui 引入thymeleaf模板引擎 1 <html lang=&qu ...

  2. Spring Boot - Thymeleaf模板简介以及集成

    文章目录 Spring Boot - Thymeleaf模板简介以及集成 1.什么是Thymeleaf? 2.标准表达式 2.1 变量表达式 2.2 选择表达式/星号表达式 2.3 URL表达式 2. ...

  3. Spring Boot开发之流水无情(二)

    本篇记录Spring Boot的几个知识点:  (一)一个Maven+Spring Boot项目基本的包结构形式  (二)一个简单的在Spring Boot项目集成安全控制  (二)如何在Spring ...

  4. Spring Boot 揭秘与实战(二) 数据缓存篇 - EhCache

    文章目录 1. EhCache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 EhCache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门 ...

  5. Spring Boot + Thymeleaf 创建web项目

    本篇文章将引导你创建一个简单的Spring Boot web程序示例,涉及到的组件有:嵌入的Tomcat + Thymeleaf 模板引擎,可执行的 JAR 文件包. 开发工具: 1.Spring B ...

  6. Spring Boot 揭秘与实战(二) 数据缓存篇 - Guava Cache

    本文,讲解 Spring Boot 如何集成 Guava Cache,实现缓存. 博客地址:blog.720ui.com/ 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门」 ...

  7. spring boot之http,页面状态跳转与异常处理实战

    spring boot之http,页面状态跳转与异常处理实战 参考文章: (1)spring boot之http,页面状态跳转与异常处理实战 (2)https://www.cnblogs.com/yu ...

  8. Spring Boot 项目的jsp页面引用css、js、img、fonts的问题解决

    Spring Boot 项目的jsp页面引用css.js.img.fonts的问题解决 我在刚开始用Spring Boot 写招聘网站时,jsp页面写好了,代码逻辑写好了,css.js.img.fon ...

  9. 【Spring Boot+Thymeleaf+MyBatis+mysql】实现电子商务平台实战(附源码)持续更新~~ 包括sql语句、java、html代码

    源码请点赞关注收藏后评论区留言和私信博主 开发环境:Web服务器使用Servlet容器,数据库采用mysql,集成开发环境为Spring Tool Suite(STS) 一.系统设计 电子商务平台分为 ...

  10. Spring Boot Thymeleaf(十一)

    一.Thymeleaf 简介 Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎.它与 JSP,Velocity,FreeMaker 等模板引擎类似,也可以轻易地与 S ...

最新文章

  1. CNN在文本分类的应用(内有代码实现) 论文Convolutional Neural Networks for Sentence Classification
  2. 第五个页面:更多电影页面
  3. 算法导论之计算几何学
  4. spring boot 异常处理
  5. 中专科学计算机应用基础试题及答案,职业中专《计算机应用基础》期中考试试卷...
  6. 卡顿人生,如何拯救?
  7. 关于eclipase出现的problems during content assist报错问题
  8. 《趣学算法 [陈小玉]》学习笔记01
  9. 九零后女孩币圈变形记
  10. 我的Android进阶之旅------Android检测wifi连接状态
  11. P版openstack-nova-compute中日志报错无法同步resource_provider
  12. 搜狗词库合集分享_Rime小狼毫
  13. 【Codecs系列】视频格式国际标准:BT601/BT709/BT2020
  14. 无线通信与编码_MATLAB仿真实现Jakes信道模型_含仿真代码_瑞利衰落信道模型
  15. 9种实用的将3.3V输出连接到5V输入的方法
  16. IDEA中解决Spring 配置文件未受管束问题,提示:Unmapped Spring configuration files found
  17. 全球地震 Python 爬虫可视化,最频发的地方是这里!
  18. 汽车“新四化”,如何扬长避短?
  19. Docker和Jenkins构建项目总结
  20. php 抢红包_用PHP实现的抢红包小程序

热门文章

  1. Top K 算法详解
  2. ThinkPHP2.1 增加PHPCMS模板引擎,支持PC标签(get,json)
  3. JS 数组 Array 对象详解 与 for...of 增强型迭代
  4. MySQL sql语句字段截取前几位,后几位等
  5. 瀚云平台kafka简单原理
  6. 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签
  7. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第4节 方法引用_6_方法引用_类的构造器(构造方法)引用...
  8. C#弹出窗体、C#导出Excel、C#数据展示框、C#弹出框
  9. js判断是对象还是集合
  10. 微信支付之异步通知签名错误