整合JSP模板
添加依赖
创建 maven 工程,在 pom.xml 文件中添加如下依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
创建目录和配置文件
1. JSP页面放在:/src/main/webapp/WEB-INF/jsp/目录下(webapp/WEB-INF/jsp 没有就自己创建),如:
/src/main/webapp/WEB-INF/jsp/index.jsp
2. CSS或JavaScript之类的静态文件:/src/main/resources/static/目录下,如:
/src/main/resources/static/css/main.css
3. 对于属性文件放在:/src/main/resources/目录下,如:
/src/main/resources/application.properties
配置application.properties
只需要添加这2个就可以了,如下:
#页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ #页面默认后缀目录 spring.mvc.view.suffix=.jsp |
或在application.yml中:spring节点下面:
spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp |
创建JSP页面文件夹
在src/main/webapp中的WEB-INF/下创建jsp文件夹,用于存放jsp页面文件。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<br>
${map}
</body>
</html>
编写Controller
package club.lygangdai.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* @author Uncle Liu
* @ClassName: TestController
* @Description: TODO(类说明 :)
* @date 2018/11/7 11:31
*/
@Controller
@RequestMapping("/jsp")
public class TestController {
@RequestMapping("/index")
public String hello(Map<String,Object> map) {
map.put("map", "Hell");
return "index";
}
}
结果如下:
整合Freemarker 模板
添加 Freemarker 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
添加 Freemarker 模板配置
在 application.properties 中添加如下内容:
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
spring.freemarker.suffix=.ftl
上述配置都是默认值。
编写Controller
package club.lygangdai.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* @author Uncle Liu
* @ClassName: FreemarkerController
* @Description: TODO(类说明 :)
* @date 2018/11/7 11:32
*/
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController{
@RequestMapping("/hello")
public String hello(Map<String,Object> map) {
map.put("map", "Hell Freemarker");
return "freemarker";
}
}
在 templates 目录中创建名为 freemarker .ftl 文件,内容如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="/css/index.css" rel="stylesheet"/>
</head>
<body>
<div class="container">
<h2>${map}</h2>
</div>
</body>
</html>
结果如下:
整合Thymeleaf 模板
添加 Thymeleaf依赖
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
添加 Thymeleaf模板配置
在 application.properties 中添加如下内容:
#thymeleaf模板
spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
上述配置都是默认值。
编写Controller
package club.lygangdai.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {
@RequestMapping("thymeleaf")
public String hello(Map<String,Object> map) {
map.put("map", "Hello Thymeleaf");
return "thymeleaf";
}
}
在 templates 目录中创建名为 thymeleaf.html 文件,内容如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2 th:text="${map}"></h2>
</body>
</html>
结果如下:
评论 (0)