3-springboot知识点-freemarker、thymeleaf、jsp

一、freemarker

  1. pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
  1. application.properties
#源码在freemarkerAutoConfiguration中的FreeMarkerProperties中
#自定义Freemarker模板位置,模板的位置默认是在resources下的templates
spring.freemarker.template-loader-path=classpath:/lyl/
#是否开启freemarker缓存(一般开发环境不开启,生产环境看情况)
spring.freemarker.cache=false
#自定义模板的编码格式,默认就是UTF-8
spring.freemarker.charset=UTF-8
#定义模板的content-type
spring.freemarker.content-type=text/html
#设置模板的后缀。默认是ftlh
#spring.freemarker.suffix=.ftl
  1. index.html
<table border="1">
        <tr>
            <td>用户id</td>
            <td>用户姓名</td>
            <td>用户地址</td>
            <td>用户性别</td>
        </tr>
        <#list users as user>
            <#if user.id==4>
                <#break>
            </#if>
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.address}</td>
                <td>
                    <#-- if指令  -->
<#--                    <#if user.gender==0><#elseif user.gender==1><#else>
                        未知
                    </#if>-->

                    <#--switch指令  -->
                    <#switch user.gender>
                        <#case 0><#break>
                        <#case 1><#break>
                        <#default>未知
                    </#switch>
                </td>
            </tr>
        </#list>
    </table>
  1. UserController
@Controller
public class UserController {

    @GetMapping("/user")
    public String user(Model model) {
        List<User> users = new ArrayList<>();
        Random random = new Random();
        for(int i = 0;i < 10 ;i++) {
            User user =  new User();
            user.setId((long) i);
            user.setName("李燕玲"+i);
            user.setAddress("重庆"+i);
            //0:男,1:女 其他:未知
            user.setGender(random.nextInt(3));
            users.add(user);
        }
        model.addAttribute("users",users);
        return "index";
    }
}

二、thymeleaf

  1. pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. BookController
@Controller
public class BookController {

    @GetMapping("/book")
    public String book(Model model) {
        List<Book> books = new ArrayList<>();
        for (int i = 0; i < 10;i++) {
            Book book = new Book();
            book.setId((long) i);
            book.setName("三国演义"+i);
            book.setAuthor("罗贯中"+i);
            book.setPrice(30.0);
            books.add(book);
        }
        model.addAttribute("books",books);
        return "book";
    }
}
  1. index.html
<!DOCTYPE html>
<!--引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
<table border="1">
    <tr>
        <td>书本ID</td>
        <td>书本名称</td>
        <td>书本作者</td>
        <td>书本价格</td>
    </tr>
    <tr th:each="book : ${books}">
        <td th:text="${book.id}"></td>
        <td th:text="${book.name}"></td>
        <td th:text="${book.author}"></td>
        <td th:text="${book.price}"></td>
    </tr>
</table>
</body>
</html>

三、jsp

  1. pom.xml
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
  1. WebMvcConfig
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    //配置视图解析器
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/jsp/",".jsp");
    }
}
  1. JspController
@Controller
public class JspController {

    @GetMapping("/jsp")
    public String jsp(Model model, String name) {
        model.addAttribute("name",name);
        //hello底下的报错,实际上是没有问题的
        return "hello";
    }
}
  1. webapp/jsp/hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsp</title>
</head>
<body>
    <h1>hello ${name}</h1>
</body>
</html>

四、添加文件模板到下图页面

在这里插入图片描述
步骤:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值