【使用篇二】SpringBoot整合jsp(6)
1. pom.xml中添加jstl和jasper
springboot不推荐使用jsp,所以在spring-boot-starter-web启动器中并没有包括这两个,所以我们需要单独引入:
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jasper:jsp引擎 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
2. 在src/main/resources目录下创建application.properties,添加jsp的视图映射
#jsp映射配置 spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
3. 编写controller
public class User {
private Integer userId;
private String userName;
private Integer age;
public User() {
}
public User(Integer userId, String userName, Integer age) {
this.userId = userId;
this.userName = userName;
this.age = age;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@RestController
public class UserController {
@RequestMapping("/findUserList")
public Object findUserList(Model model){
List<User> list = new ArrayList<User>();
list.add(new User(1, "张三", 20));
list.add(new User(2, "李四", 27));
list.add(new User(3, "王五", 30));
model.addAttribute("list",list);
//因为使用@RestController注解,所以要返回视图,必须使用ModelAndView
ModelAndView view = new ModelAndView();
view.addObject(model);
view.setViewName("userList");//视图名称
return view;
}
}
4. 在src/main下创建webapp
前面在application.properties中,已添加了jsp的视图映射,所以在webapp目录下创建WEB-INF/jsp,并创建userList.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户列表</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.userId }</td>
<td>${user.userName }</td>
<td>${user.age }</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
5. 编写启动类,执行main方法,访问浏览器:http://localhost:8080/findUserList

6. IDEA下的注意事项
以上是在Eclipse工具下开发的。如果大家用的是IDEA,需要特别注意其中一个项目配置,否则可能找不到jsp页面。


其次,使用jsp的页面还是设置pom.xml的<packaging>war</packaging>。
【使用篇二】SpringBoot整合jsp(6)的更多相关文章
- SpringBoot整合Jsp和Thymeleaf (附工程)
前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...
- springboot整合jsp模板
springboot整合jsp模板 在使用springboot框架里使用jsp的时候,页面模板使用jsp在pom.xnl中需要引入相关的依赖,否则在controller中无法返回到指定页面 〇.搭建s ...
- jackson学习之十(终篇):springboot整合(配置类)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 1、SpringBoot整合之SpringBoot整合JSP
SpringBoot整合JSP 一.创建SpringBoot项目,仅选择Web模块即可 二.在POM文件中添加依赖 <!-- 添加servlet依赖模块 --> <dependenc ...
- Canal 实战 | 第一篇:SpringBoot 整合 Canal + RabbitMQ 实现监听 MySQL 数据库同步更新 Redis 缓存
一. Canal 简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同 ...
- 03-01:springboot 整合jsp
1.修改pom文件,添加坐标 <!-- jstl --> <dependency> <groupId>javax.servlet ...
- SpringBoot整合jsp技术
1.修改pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- Idea中SpringBoot整合JSP
最近在学习SpringBoot,看到SpringBoot整合jsp,顺带记录一下. 1.创建一个SpringBoot项目 点击Next 注意:packaging选中War,点击Next Webà选中W ...
- 【SpringBoot】08.SpringBoot整合jsp
SpringBoot整合jsp 1.修改pom文件加入两个坐标jstl标签库和jasper <project xmlns="http://maven.apache.org/POM/4. ...
- springboot整合jsp,完成公交车站路线图
转: springboot整合jsp,完成公交车站路线图 点赞再看,养成习惯 开发环境: jdk 8 intellij idea tomcat 8 mysql 5.7 maven 3.6 所用技术: ...
随机推荐
- Python IO 模式
IO 模式 对于 Linux 的 network IO: 一次 IO 访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区 copy 到应用程序的地址空间.所 ...
- 解释JUnit中@BeforeClass和@AfterClass标注的方法必须是static的,而在TestNg不必
在JUnit中@BeforeClass和@AfterClass标注的方法必须是static的:但是在TestNg中却没有这样的限制,这是为什么呢. 其实和他们两的运行机制有关. 在junit中: 每运 ...
- 洛谷P2508 [HAOI2008]圆上的整点
题目描述 求一个给定的圆$ (x^2+y^2=r^2) $,在圆周上有多少个点的坐标是整数. 输入格式 \(r\) 输出格式 整点个数 输入输出样例 输入 4 输出 4 说明/提示 \(n\le 20 ...
- Metersploit系统参数说明
Back参数 Back参数主要⽤用于返回.⽐比如你进⼊入了了某⼀一个漏漏洞洞模块的设置,但是你想再重新选择一个漏漏洞洞模块,那么就需要⽤用到back参数. 这张图说明,才开始我使用了ms08_067_ ...
- 二分图学习记 之 KM算法 二分图最大权完美匹配。
前置知识 :匈牙利算法 首先有这样一张图,求这张图的最大权完美匹配. 当然如果你不想看这些渣图的话,您可以转到 洛谷 运动员最佳匹配问题 下面我来强行解释一下KM算法 左边一群妹子找汉子,但是每个妹子 ...
- springboot只能一个main方法解决办法
pom.xml修改properties,增加这行 <start-class>com.eshore.main.SpringBootStarter</start-class> 或者 ...
- 【09】Nginx:静态压缩 / 日志切割 / 防盗链 /恶意解析/ 跨域
写在前面的话 上一节我们谈了关于 nginx 服务器的一些简单的安全优化问题,能够帮助我们解决一部分线上服务存在的安全隐患.但是想要提升用户体验这是原因不够的,我们还需要从服务的优化方面入手. 本节更 ...
- SpringBoot多数据源动态切换数据源
1.配置多数据源 spring: datasource: master: password: erp_test@abc url: jdbc:mysql://127.0.0.1:3306/M201911 ...
- 转 OpenCV Mat 数据读写
转:https://blog.csdn.net/u011520181/article/details/83831866 1.创建 Mat 对象: // 创建一个 320x240 的 8 位无符号型 4 ...
- 算法初步---基本的数据结构(java为例)
最近搞算法,觉得超级吃力的,一直以为数学好的,数学可以考试满分,算法一定没什么问题,贱贱地,我发现我自己想多了,还是自己的基础薄弱吧,今天我来补补最基础的知识. 算法(Algorithm)是指解题方案 ...