新建工程
配置如下
点击下一步
点击完成,完成创建
在初始化完成后的目录结构如下所示
现在填充目录
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父级依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/>
</parent>
<!-- 项目包结构 -->
<groupId>com.liu</groupId>
<!-- 项目工件名 -->
<artifactId>springboot_day1</artifactId>
<!-- 项目版本 -->
<version>0.0.1-SNAPSHOT</version>
<!-- 项目名 -->
<name>springboot_day1</name>
<!-- 项目描述 -->
<description>这是我学习springboot和jsp整合的demo</description>
<!-- 打包方式 -->
<packaging>war</packaging>
<!-- JDK1.8 -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 项目依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加servlet依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--jsp页面使用jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<!--这里必须是META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<includes>
<!--以任意开头点任意结尾的文件-->
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8181
spring:
mvc:
view:
prefix: /
suffix: .jsp
ControllerTest
@Controller
@RequestMapping("/test")
public class ControllerTest {
@RequestMapping("/index")
public String hello() {
System.out.println("Hello >>>");
return "index";
}
}
index.jsp
<html>
<body>
<h1>hello spring boot and jsp</h1>
</body>
</html>
启动,测试
快速写一个 Spring Boot 的 CRUD
包结构如下
User实体类
@Data
@AllArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父级依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/>
</parent>
<!-- 项目包结构 -->
<groupId>com.liu</groupId>
<!-- 项目工件名 -->
<artifactId>springboot_day1</artifactId>
<!-- 项目版本 -->
<version>0.0.1-SNAPSHOT</version>
<!-- 项目名 -->
<name>springboot_day1</name>
<!-- 项目描述 -->
<description>这是我学习springboot和jsp整合的demo</description>
<!-- 打包方式 -->
<packaging>war</packaging>
<!-- JDK1.8 -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 项目依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加servlet依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--jsp页面使用jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<!--这里必须是META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<includes>
<!--以任意开头点任意结尾的文件-->
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserService接口
public interface UserService {
Collection<User> findAll();
User findById(long id);
void add(User user);
void deleteById(long id);
void update(User user);
}
UserServiceImpl
这里使用 HashMap 模拟数据库
@Service
public class UserServiceImpl implements UserService {
private static HashMap<Long, User> users;
static {
users = new HashMap<>();
users.put(1L, new User(1, "小刘", 12));
users.put(2L, new User(2, "李白", 34));
users.put(3L, new User(3, "王昭君", 22));
users.put(4L, new User(4, "貂蝉", 16));
}
@Override
public Collection<User> findAll() {
return users.values();
}
@Override
public User findById(long id) {
return users.get(id);
}
@Override
public void add(User user) {
users.put(user.getId(), user);
}
@Override
public void deleteById(long id) {
users.remove(id);
}
@Override
public void update(User user) {
users.put(user.getId(), user);
}
}
UserController 控制器
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/index")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("Users", userService.findAll());
return modelAndView;
}
@GetMapping("/findAll")
@ResponseBody
public Collection<User> findAll() {
System.out.println("查询所有的接口");
return userService.findAll();
}
@GetMapping("/findById/{id}")
public ModelAndView findById(@PathVariable("id") long id) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user", userService.findById(id));
modelAndView.setViewName("update");
return modelAndView;
}
@PostMapping("/save")
public String save(User user) {
System.out.println("保存接口 USer:" + user);
userService.add(user);
return "redirect:/user/index";
}
@GetMapping("/deleteById/{id}")
public String delete(@PathVariable("id") long id) {
System.out.println("删除接口 id" + id);
userService.deleteById(id);
return "redirect:/user/index";
}
@PostMapping("/update")
public String update(User user) {
System.out.println("更新接口 User:" + user);
userService.update(user);
return "redirect:/user/index";
}
}
SpringbootDay1Application
@SpringBootApplication
public class SpringbootDay1Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDay1Application.class, args);
}
}
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table>
<h1>用户信息</h1>
<table>
<tr>
<th>用户编号</th>
<th>用户姓名</th>
<th>用户年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${Users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>
<a href="/user/findById/${user.id}">修改</a>
<a href="/user/deleteById/${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="/add.jsp">添加用户</a>
</table>
</body>
</html>
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/user/save" method="post">
<input type="text" name="id" value="${user.id}">
<input type="text" name="name" value="${user.name}">
<input type="text" name="age" value="${user.age}">
<input type="submit">
</form>
</body>
</html>
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/user/update" method="post">
<input type="text" name="id" value="${user.id}" readonly>
<input type="text" name="name" value="${user.name}">
<input type="text" name="age" value="${user.age}">
<input type="submit">
</form>
</body>
</html>
启动测试