SpringBoot的Web开发支持与Thymeleat模板引擎

SpringBoot的Web开发支持与Thymeleat模板引擎

Web开发是基于B/S架构的应用软件开发技术,分为前端(用户接口)和后端(业务逻辑和数据),前端的可视化及用户交互由浏览器实现,即以浏览器作为客户端,实现客户与服务器进行远程交互数据。SpringBoot的Web开发内容主要包括内嵌的Servlet容器和SpringMVC.

SpringBoot提供了Spring-boot-starter-web依赖模块,该模块包含SpringBoot预定义的Web开发依赖包,为Web开发者提供内嵌的Servlet容器以及SpringMVC的依赖,如果开发者希望开发SpringBoot的Web应用程序,可以在SpringBoot项目的pom.xml中田添加依赖配置。SpringBoot将自动关联web开发的相关依赖,如tomcat,SpringMVC等,进而对web开发提供支持,并对相关技术的配置实现自动配置。

在SpringBoot的Web应用中,建议开发者使用HTML完成动态页面,SpringBoot提供了许多的模板引擎,因为Thymeleaf提供了完美的SpringMVC支持,所以在SpringBoot的web应用中推荐使用Thymeleaf作为模板引擎。

ThymeLeaf是一个Java类库,是一个XML/HTML的模板引擎,能够处理HTML,XML,JS,以及CSS,可以作为MVCWeb应用的View层显示数据。

下面学习一个基于Thymeleaf模板引擎的SpringBoot Web应用
1-创建Maven项目,并在pom.xml文件中添加依赖。

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>Thymeleaf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <!--配置SpringBoot的核心启动器-->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
    <dependency>
        <!--添加starter模块-->
        <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>

</project>

2-在src/main/java目录下创建一个名称为com.controller的包,在该包中创建控制器类。


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestThymeleafController {
    @RequestMapping("/index")
    public String test(){
        return "index" ;
    }

}

3-在src/main/resources/templates目录下新建index.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
测试SpringBoot的Thymeleaf支持
</body>
</html>

4-在src/mian/java包下创建包com.test在该包中创建启动类。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com"})
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args) ;
    }
}

5-运行启动类,访问http://localhost:8080/index
在这里插入图片描述
springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf。

下面学校IDEA自动创建SpringBoot的web项目。
在这里插入图片描述
在这里插入图片描述
选择web项目
在这里插入图片描述
设置项目名
在这里插入图片描述
项目创建完成,结构如下:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值