SpringBoot & Thymeleaf项目实战

web模板语言-thymeleaf

thymeleaf

项目启动,及各个页面入口

项目地址

tinytongtong/spring-thymeleaf

第一步项目运行:

打开IDE,将EurekaServerApplication执行起来。

访问接口-浏览器直接访问

eg: get请求:
http://localhost:8201/api/execute/get

http://localhost:8201/api/execute/get?name=maolegemi

访问web页面
访问静态页面:

http://localhost:8201/static.html

访问动态页面:
1、访问index页面

http://localhost:8201/index

2、访问basic-usage页面

http://localhost:8201/basic-usage

3、访问request-methods页面

http://localhost:8201/request-methods

项目架构

应用入口类-EurekaServerApplication

启动该类,即可启动服务。

在这里插入图片描述

web页面对应WebController

在这里插入图片描述

api接口对应ApiController

在这里插入图片描述

Web静态页面

resources/static/目录下的资源

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nZtwBto7-1632454296976)(evernotecid://5E413247-DCB6-499B-8453-F2A2C2DF33A4/appyinxiangcom/13182898/ENResource/p17323)]

Web动态页面

resources/template/目录下的资源

在这里插入图片描述

web动态页面配置

SpringBoot使用的thymeleaf,具体看·项目根目录/src/main/resources/appliaction.yml`.

在这里插入图片描述

如何新建页面

新建html文件

resources/template/目录下新建html文件,html代码模板:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <script th:src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
    <title>Title</title>
</head>
<body>

<div>
    
</div>

<script>
    
</script>

</body>
</html>

第一次添加,需要将这个uri添加进配置:

在这里插入图片描述

js相关依赖:在pom.xmlproject#dependencies/dependency标签下:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7-1</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.1.1</version>
</dependency>
在WebController增加对应的路由方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uza3uKxP-1632454296979)(evernotecid://5E413247-DCB6-499B-8453-F2A2C2DF33A4/appyinxiangcom/13182898/ENResource/p17197)]
接着添加

    /**
     * 本地访问内容地址 :http://localhost:8201/request-methods
     *
     * @param map
     * @return
     */
    @RequestMapping("/request-methods")
    public String requestMethods(HashMap<String, Object> map, Model model) {
        model.addAttribute("get111", "我是get方法");
        return "request-methods";// html页面名称-request-methods.xml
    }

如何跳转页面

通过相对路由地址:

<button onclick="window.location.href='/request-methods'">跳转request-methods页面</button>

如何发送请求-ajax

页面内部应该发送ajax请求。

AJAX-廖雪峰

端口占用问题

查看端口占用
lsof -i:8201
关闭端口当前进程
kill -9 pid
Spring Boot + Thymeleaf是一个常见的轻量级Web开发框架组合,适合快速构建MVC架构的应用。Thymeleaf是一种现代、强大且易于使用的模板引擎,它允许动态HTML5页面的生成。 一个实战案例可能是创建一个博客管理系统: 1. **设置环境**:首先,你需要创建一个新的Spring Boot项目,并添加Thymeleaf依赖到pom.xml文件中。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. **配置视图解析器**:在application.properties文件中配置Thymeleaf视图的位置和默认模板Engine。 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 3. **创建控制器**:编一个控制器,如`BlogController`,处理HTTP请求,如展示博客列表、单篇博客详情以及发布新博客。 4. **创建模板**:在`templates`目录下创建HTML模板,如`blog/list.html`用于显示博客列表,`blog/detail.html`用于详细内容,`blog/create.html`用于创建表单。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <!-- ... --> </head> <body> <!-- 显示博客列表 --> <ul th:each="blog : ${blogs}"> <li th:text="${blog.title}"></li> </ul> </body> </html> ``` 5. **模型绑定**:在控制器方法中,使用ModelAndView来将数据传递给模板。例如,在展示博客列表时,可以注入`@ModelAttribute`来接收用户输入。 ```java @GetMapping("/blogs") public String showBlogs(@ModelAttribute("blogs") List<Blog> blogs) { return "blog/list"; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tinyvampirepudge

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值