Spring boot 整合jsp、thymeleaf、freemarker
1.创建spring boot 项目
2、pom文件配置如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
3、编写controller
@Controller
public class ShowUserController {
@RequestMapping("/showuser")
public String showUser(Model model) {
List<user> list = new ArrayList<user>();
list.add(new user("1","张三","23"));
list.add(new user("2","李斯","26"));
list.add(new user("3","汪汪","25"));
list.add(new user("4","嘿咻","24"));
model.addAttribute("userList",list);
//thymeleaf 例子
return "thymeleaf";
//freemarker 例子
//return "userShow";
//jsp 例子
//return "userindex";
}
}
4、页面
(1)thymeleaf.html
<html>
<head>
<title>springboot-freemarker测试</title>
<meta charset='utf-8'/>
</head>
<body>
<table border=1 align="center" width="50%">
<tr>
<td>ID</td>
<td>NAME</td>
<td>AGE</td>
</tr>
<tr th:each=" user : ${userList} ">
<td th:text="${user.userId}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.userAge}"></td>
</tr>
</table>
</body>
</html>
(2)userShow.ftl
<html>
<head>
<title>springboot-freemarker测试</title>
<meta charset='utf-8'/>
</head>
<body>
<table border=1 align="center" width="50%">
<tr>
<td>ID</td>
<td>NAME</td>
<td>AGE</td>
</tr>
<#list userList as user >
<tr>
<td th:text="${user.userId}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.userAge}"></td>
</tr>
</#list>
</table>
</body>
</html>
(3)userindex.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>用户展示</title>
</head>
<body>
<table border=1 align="center" width="50%">
<tr>
<td>ID</td>
<td>NAME</td>
<td>AGE</td>
</tr>
<c:forEach var="user" items="${userList}" >
<tr>
<td>${user.userId}</td>
<td>${user.userName}</td>
<td>${user.userAge}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
注:jsp 额外配置 application.properties 文件
//jsp 位置
spring.mvc.view.prefix=/WEB-INF/jsp/
//jsp后缀
spring.mvc.view.suffix=.jsp
5编写启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
分别运行调试。
注意各文件位置
本人初学。请各位大佬多多指点。谢谢
最新文章
- Python基础+Pythonweb+Python扩展+Python选修四大专题 超强麦子学院Python35G视频教程
- 【vijos1243】 生产产品
- oracle触发器设置uuid变量
- Sublime Text 3 3126 注册码
- c#和数据库技术基础之程序设计集散地-数据库
- Extjs4.2.1中的helloworld
- 解决数据标准及质量AppScan(测试)安全性问题相关方法
- Systemd 入门教程:命令篇
- Windows玩转Docker(二):运行whalesay image
- 一个人ACM(我们赶上了ACM)
- EM and GMM(Code)
- 微软Skype Linux客户端全新发布
- MongoDB更需要好的模式设计 及 案例赏析
- Perl 正则匹配经验记录
- 【PgSQL安装(含配置)】PostgreSQL简称PgSQL,是1980以加利福尼亚大学开发的DBMS,严格遵守标准SQL。
- perl 函数
- 如何让移植的嵌入式ARM显示中文汉字
- SVN 基本的工作循环
- Android 判断是否是Rtl
- kibana 安装