当前位置: 首页 > news >正文

Spring Boot + thymeleaf 后台与页面(二)

 

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.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
spring.datasource.username = root
spring.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即可

 

 

相关文章:

  • vue学习系列(二)vue-cli
  • java8简短教程(持续更新含部分9,10,11)
  • Kali linux 2018安装后全屏乱码解决
  • SAP云平台对Kubernetes的支持
  • Centos6.5配置DNS
  • 机器学习你要了解的5件事
  • web开发中的跨域整理
  • Kafka连接器深度解读之JDBC源连接器
  • java面试-深入理解JVM(四)——对象内存的分配策略
  • laravel中使一段文字,限制长度,并且超出部分使用指定内容代替
  • AWS提高声音辨识精确度为解决ML训练数据平衡性
  • iframe的高度自适应问题
  • Linux下关闭防火墙命令
  • App Store审核指南(苹果官方)(转)
  • 深入理解 async / await
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 【译】理解JavaScript:new 关键字
  • 10个确保微服务与容器安全的最佳实践
  • 30天自制操作系统-2
  • Android Volley源码解析
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • node学习系列之简单文件上传
  • Swift 中的尾递归和蹦床
  • vue:响应原理
  • Zepto.js源码学习之二
  • 大快搜索数据爬虫技术实例安装教学篇
  • 对象管理器(defineProperty)学习笔记
  • 工作中总结前端开发流程--vue项目
  • 开源SQL-on-Hadoop系统一览
  • 前端攻城师
  • 如何编写一个可升级的智能合约
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 写给高年级小学生看的《Bash 指南》
  • 一起参Ember.js讨论、问答社区。
  • Spring第一个helloWorld
  • zabbix3.2监控linux磁盘IO
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #pragma预处理命令
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (一)UDP基本编程步骤
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (转)c++ std::pair 与 std::make
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)EXC_BREAKPOINT僵尸错误
  • (转载)Linux网络编程入门
  • (转载)OpenStack Hacker养成指南
  • . NET自动找可写目录
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET CF命令行调试器MDbg入门(一)
  • .NET 发展历程
  • .NET 事件模型教程(二)