一、SpringBoot整合jsp

在maven的dependencies的依赖中除了springBoot启动器还要添加对jstl和jsp的依赖。

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependendy>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

在application.properties中修改jsp全局访问设置

spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

接下来就只需要在controller中返回spring类型的视图名称即可访问到WEB-INF/jsp/中相应的jsp文件

书写controller

 @Controller
public class UserController {
@RequestMapping("/showUser")
public String showUser(Model model) {
List<User> userList = new ArrayList<>();
userList.add(new User("鬼魅传说"));
userList.add(new User("魑魅魍魉"));
userList.add(new User("妖魔"));
model.addAttribute("userList", userList);
return users;
}
}

书写WEB-INF/jsp/users.jsp

 <%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@tagbib url="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

二、SpringBoot整合Freemarker

在maven中除了添加springboot启动器外,还要添加对freemarker的依赖

 <dependencies>
<dependency>
<groupId>org.springframework.boot<groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>

springBoot要求模板形式的视图层技术的文件必须放到src/main/resources目录下必须要有一个名称为templates的文件夹

编写controller,同上也是把userList放到model的attribute中即可

然后写freemarker模板即可

 <html>
<head>
<meta chaset="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>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</#list>
</table>
</body>
</html>

三、springboot整合Thymeleaf

在maven中添加对thymeleaf的依赖。

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependecies>

视图存放的位置为src/main/resources/templates,该目录是安全的,意味着该目录下的内容不允许被外界直接访问。

编写controller同上,在model中添加attributeute的userList即可

编写前端视图src/main/resources/templates/users.html

 <html>
<head>
<meta charset="utf-8">
</head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
<tr th:each="user : ${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
</body>
</html>

SpringBoot视图层技术的更多相关文章

  1. SpringBoot_整合视图层技术

    SpringBoot整合视图层技术 在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地.Spring Boot对视图层技术提供了很好的支持,官方推荐使用的模板引擎是Thymele ...

  2. Spring Boot 整合视图层技术

    这一节我们主要学习如何整合视图层技术: Jsp Freemarker Thymeleaf 在之前的案例中,我们都是通过 @RestController 来处理请求,所以返回的内容为json对象.那么如 ...

  3. Spring Boot 整合视图层技术,application全局配置文件

    目录 Spring Boot 整合视图层技术 Spring Boot 整合jsp Spring Boot 整合freemarker Spring Boot 整合视图层技术 Spring Boot 整合 ...

  4. SpringBoot持久层技术

    一.Springboot整合mybatis maven中添加对数据库与mybatis的依赖 <dependencies> <dependency> <groupId> ...

  5. 整合Freemarker视图层和整合jsp视图层和全局捕获异常

    SpringBoot静态资源访问 1.静态资源:访问 js / css /图片,传统web工程,webapps springboot 要求:静态资源存放在resource目录下(可以自定义文件存放) ...

  6. Django 的路由层 视图层 模板层

    --------------------------------------------------------------通过苦难,走向欢乐.——贝多芬 Django-2的路由层(URLconf) ...

  7. JSF Web框架与Facelets表现层技术

    JSF(JavaServer Faces) JSF应用程序的生命周期从客户端对页面发出HTTP请求时开始,并在服务器响应页面时结束.JSF生命周期分为运行阶段和渲染阶段两个主要阶段. 执行阶段 当第一 ...

  8. RGBD动作识别的多视图层融合模型

    摘要 基于视觉的动作识别在实践中遇到了不同的挑战,包括从任何角度识别主题,实时处理数据以及在现实环境中提供隐私.甚至识别基于配置文件的人类动作(基于视觉的动作识别的一个子集),在计算机视觉中也是一个巨 ...

  9. 表现层技术以及Freemaker使用教程

    表现出计数以及Freemaker详解 在java领域,表现层技术主要有三种:jsp.freemarker.velocity.jsp是大家最熟悉的技术优点: 1.功能强大,可以写java代码 2.支持j ...

随机推荐

  1. Linux 之 文件

    文件名称 在linux中,windows概念中的文件夹和文件是没有区别的,都是统称为文件. 1.Linux中文件的名称大小写是敏感的 2.名称最多可以为255个字符 3.除了正斜线以外,都是有效字符 ...

  2. 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作

    准备 使用框架搭建完成项目,包含OF.WinService项目. 了解Window Service 和定时服务相关知识. 一.添加一个定时服务 第一步:了解项目结构 第二步:创建一个新的Job 第三步 ...

  3. springboot ElasticSearch 简单的全文检索高亮

    原文:https://segmentfault.com/a/1190000017324038?utm_source=tag-newest 首先引入依赖 <dependency> <g ...

  4. C++中priority_queue的用法

    本来想自己写一写的,但看到这个随笔,感觉要写的东西跟这个差不多,就直接附上链接. 需要注意事项: rand()函数需要引入头文件#include<cstdlib>. 自定义类型,重载ope ...

  5. Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)

    题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个 ...

  6. k8s的网络

    K8S的网络中主要存在4种类型的通信:   ①同一Pod内的容器间通信 ②各个Pod彼此间的通信 ③Pod和Service间的通信 ④集群外部流量和Service之间的通信   K8S为Pod和Ser ...

  7. 【数组模拟的链表or复杂模拟】PAT-L2-002. 链表去重

    L2-002. 链表去重 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须 ...

  8. Android上执行python脚本-QPython

    看书,发现android可以跑python. 尝试了一下. 首先需要在手机上安装python环境,通过安装apk实现,这个apk叫QPython,还有同类的比如SL4A. QPython的官网:htt ...

  9. python xlwt 设置单元格样式

    使用xlwt中的Alignment来设置单元格的对齐方式,其中horz代表水平对齐方式,vert代表垂直对齐方式. VERT_TOP = 0x00 上端对齐 VERT_CENTER = 0x01 居中 ...

  10. 简单聊一下对MySQL索引的理解?

    一.索引是什么? 索引是帮助MySQL高效获取数据的数据结构. 二.索引能干什么? 索引非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索引能够轻易将查询性能提高好几个数量级, ...