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

整合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>
posted @ 2019-09-24 10:24  fly_bk  阅读(180)  评论(0编辑  收藏  举报