07.整合jsp、整合freemarker、整合thymeleaf

本文详细介绍如何在Spring Boot中整合并配置JSP、Freemarker及Thymeleaf三种视图技术,包括Maven依赖、配置参数及模板示例。
整合jsp

pom.xml部分内容

 <packaging>war</packaging>
 </dependencies>
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!--用于编译jsp -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

src/main/webapp/WEB-INF/index.jsp

<body>
    <h4>${name}</h4>
</body>
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Index2Controller {
    @RequestMapping("/index")
    public String show(Model model){
        model.addAttribute("name","李四");
        return "index";
    }
}
整合freemarker
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

src/main/resources/templates

# freemarker静态资源配置
# 设定ftl文件路径 #默认是就是classpath:/templates 此条可以省略
spring.freemarker.template-loader-path=classpath:/templates
# 默认false 关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
# 默认utf-8
spring.freemarker.charset=utf-8
# 默认true
spring.freemarker.check-template-location=true
# 默认text/html
spring.freemarker.content-type=text/html
# 默认false
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
# 默认false
spring.freemarker.expose-session-attributes=false
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

index.ftl

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>index</title>
</head>
<body>
<h4>index.ftl</h4>
<h4>${name}</h4>
</body>
</html>
整合thymeleaf
       <!--引入thymeleaf的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
    </dependencies>
# 关闭缓存,开发时使用
spring.thymeleaf.cache=false
# 检查模版是否存在,然后再呈现,默认true
spring.thymeleaf.check-template-location=true
# 模版编码 默认HTML5,是严格html5校验,LEGACYHTML5依赖nekohtml
spring.thymeleaf.mode=LEGACYHTML5
# 默认classpath:/templates/
spring.thymeleaf.prefix=classpath:/templates/
# 默认.html
spring.thymeleaf.suffix=.html

index.html

<body>
    <h1 th:text="${name}"></h1>
</body>

转载于:https://www.cnblogs.com/fly-book/p/11576839.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值