整合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;@Controllerpublic 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 关闭缓存,及时刷新,上线生产环境需要修改为truespring.freemarker.cache=false# 默认utf-8spring.freemarker.charset=utf-8# 默认truespring.freemarker.check-template-location=true# 默认text/htmlspring.freemarker.content-type=text/html# 默认falsespring.freemarker.expose-request-attributes=falsespring.freemarker.expose-spring-macro-helpers=false# 默认falsespring.freemarker.expose-session-attributes=falsespring.freemarker.request-context-attribute=requestspring.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# 检查模版是否存在,然后再呈现,默认truespring.thymeleaf.check-template-location=true# 模版编码 默认HTML5,是严格html5校验,LEGACYHTML5依赖nekohtmlspring.thymeleaf.mode=LEGACYHTML5# 默认classpath:/templates/spring.thymeleaf.prefix=classpath:/templates/# 默认.htmlspring.thymeleaf.suffix=.html
index.html
<body> <h1 th:text="${name}"></h1></body>