一、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. unity图片保留周边,中间延伸

    1.先把图片切割,类似下面这样的 2.然后在使用的时候(选择图片类型的时候选择sliced)

  2. CI,CD理解

    一.什么是CI,CD ​ 当我们在谈论现代的软件编译和发布流程的时候,经常会听到CI 和CD这样的缩写短语.CI很容易理解,就是持续集成. ​ 但是CD既可以指代码持续交付,也可理解为代码持续部署.C ...

  3. Pyspark笔记一

    1. pyspark读csv文件后无法显示中文 #pyspark读取csv格式时,不能显示中文 df = spark.read.csv(r"hdfs://mymaster:8020/user ...

  4. javascript方法注释

    参考资源 文章标题:Javascript注释规范 文章地址:https://blog.csdn.net/lianlin21212411/article/details/78530913 /** * @ ...

  5. Luogu P1114 “非常男女”计划/Luogu P2697 宝石串

    Luogu P1114 "非常男女"计划/Luogu P2697 宝石串 (感觉我最近很爱做双倍经验的题啊) 使$d$等于第$i$个位置男生数(绿宝石数)减女生数(红宝石数)的差值 ...

  6. U-Net网络的Pytorch实现

    1.文章原文地址 U-Net: Convolutional Networks for Biomedical Image Segmentation 2.文章摘要 普遍认为成功训练深度神经网络需要大量标注 ...

  7. Goodbye Microservices: From 100s of problem children to 1 superstar

    https://segment.com/blog/goodbye-microservices/ Unless you’ve been living under a rock, you probably ...

  8. 运维CMDB建设思路

    在我们日常的运维工作中,面对着大量的基础设施和软件服务,该如何管理?这个管理的原则又是什么?粒度该如何控制?我们是否可以建立一个统一的标准模型来管理以上对象?管理过程中,如何降低人力成本?资源对象的生 ...

  9. npm的安装,升级与卸载

    npm查询版本 npm -v npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [npm install -g xxx]利用npm安装全局模块xxx ...

  10. rsync详细解读

    本文通过示例详细分析rsync算法原理和rsync的工作流程,是对rsync官方技术报告和官方推荐文章的解释.本文不会介绍如何使用rsync命令(见rsync基本用法),而是详细解释它如何实现高效的增 ...