Spring Boot—06集成前端模板thymeleaf
2023-09-11 14:19:52 时间
Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
application.properties
spring.thymeleaf.cache=true spring.thymeleaf.enabled=true
后端Controller类
package com.smartmap.sample.ch1.controller.view; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/system") public class MainViewController { @RequestMapping("") public String index(@RequestParam(required = false, name = "sessionId") String sessionId, Model model) { if (sessionId == null || sessionId.equals("")) { return "redirect:/system/login.html"; // return "forward:/system/login.html"; } else { String osName = System.getProperty("os.name"); model.addAttribute("name", "hello world"); model.addAttribute("host", osName); return "index"; } } @RequestMapping("/login.html") public String login(@RequestParam(required = false, name = "username") String username, @RequestParam(required = false, name = "password") String password, Model model) { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { return "login.html"; } else { return "redirect:/system?sessionId=12345"; } } }
前端index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> <h1 th:text="${host}">Hello World</h1> </body> </html>
目录结构
相关文章
- Spring Boot之Swagger2集成
- Spring Boot整合 JdbcTemplate
- Spring Boot 集成MyBatis
- Spring Boot启动时数据库初始化spring.datasource(转)
- Spring Boot 负载均衡之外置session状态保存
- spring boot集成redis
- [Spring Boot] @Component, @AutoWired and @Primary
- spring boot:集成支付宝(沙箱环境)(spring boot 2.4.0 / wap/h5方式 )
- spring boot:方法中使用try...catch导致@Transactional事务无效的解决(spring boot 2.3.4)
- spring boot:接口站增加api版本号后的安全增强(spring boot 2.3.3)
- Spring Boot的自动配置、Command-line Runner
- Docker with Spring Boot
- Spring Boot in Kotlin实践
- 【项目实战】Spring Boot集成JavaMailSender发送邮件
- 如何使用Spring Cloud搭建高可用的Elasticsearch集群?详解Elasticsearch的安装与配置及Spring Boot集成的实现
- Spring Boot 之spring.factories
- 一文详说Spring Boot集成RabbitMQ,细说消息队列常用5种模式,消息持久化,两种延迟发送的机制以及解决Spring Boot集成RabbitMQ的常见问题
- Spring Boot 集成 Spring Security 实现权限认证模块
- spring boot集成ehcache 2.x 用于hibernate二级缓存
- Spring Boot系列——7步集成RabbitMQ