Spring Boot---(7)SpringBoot整合Thymeleaf(对Thymeleaf的string,if,switch,list,map,作用域进行介绍使用)

SpringBoot整合Thymeleaf

在介绍Thymeleaf前,说一下为什么JSP为什么会被淘汰。

jsp的不足之处

有几个原因,一般公司都是会有UI设计,前端开发,后端开发。

前端将页面写好提供给后端使用的时候,你后端使用jsp,需要修改html的后缀为jsp后缀,并且,后端还会使用上el表达式,C标签之类的。这样,你做修改,前端和后端就会很麻烦。要么是前端要多了解一下jsp,要么就是后端做很大的改动。

jsp其实在java中也是属于servlet。因为jsp会编译成servlet,编译成class文件,后端才能识别使用。

jsp依赖web容器。不能单纯的打开。

介绍Thymeleaf

Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎。

Thymeleaf的主要目标是将优雅的自然模板带到您的开发工作流程中—HTML能够在浏览器中正确显示,并且可以作为静态原型,从而在开发团队中实现更强大的协作。Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

Thymeleaf的主要目标是提供一个优雅和高度可维护的创建模板的方式。 为了实现这一点,它建立在自然模板的概念之上,以不影响模板作为设计原型的方式将其逻辑注入到模板文件中。 这改善了设计沟通,弥合了前端设计和开发人员之间的理解偏差。

Thymeleaf也是从一开始就设计(特别是HTML5)允许创建完全验证的模板。

 

开始整合Thymeleaf

pom.xml

        <!-- web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- thymeleaf依赖包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Controller

package com.kevin.controller;

import com.kevin.model.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author kevin
 * @version 1.0
 * @description     springboot整合Thymeleaf的案例
 * @createDate 2019/3/14
 */
@Controller
public class DemoController {

    /**
     * Thymeleaf中的字符串案例
     * 访问地址     http://localhost:8080/show
     * @param model
     * @return
     */
    @RequestMapping("/show")
    public String showInfo(Model model){
        model.addAttribute("msg","Thymeleaf第一个案例");
        model.addAttribute("key", new Date());  // 测试日期
        return "string";
    }

    /**
     * Thymeleaf中的条件判断
     * 访问地址     http://localhost:8080/show2
     * @param model
     * @return
     */
    @RequestMapping("/show2")
    public String showInfo2(Model model){
        model.addAttribute("sex", "女");
        model.addAttribute("id","1");
        return "ifAndSwitch";
    }

    /**
     * Thymeleaf中遍历List
     * 访问地址     http://localhost:8080/show3
     * @param model
     * @return
     */
    @RequestMapping("/show3")
    public String showInfo3(Model model){
        List<Users> list = new ArrayList<>();
        list.add(new Users(1,"张三",20));
        list.add(new Users(2,&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值