Spring Boot整合JSP --CRUD
Springboot整合JSP
spring boot与视图层次的整合:
JSP 效率低
Thymeleaf
java Server page 是Java提供的一种动态的网页技术,低层是Servlet,可以直接在HTML中插入Java代码
JSP的底层的原理:
JSP是一种中间层的组件,开发者可以在这个组件中将java代码,与html代码进行整合,有jsp的引擎组件转为Servlet,再把开发者定义在组件的混合代码翻译成Servlet的相应语句,输出给客户端。
1.创建工程:基于maven的项目
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- Spring boot父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<!-- Spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.26</version>
</dependency>
</dependencies>
2.创建Handler
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("mess","hello spring boot");
return modelAndView;
}
}
3.JSP
<%--
Created by IntelliJ IDEA.
User: 郝泾钊
Date: 2022-04-10
Time: 13:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>index</h1>
${mess}
</body>
</html>
4.application.yml
server:
port: 8181
spring:
mvc:
view:
prefix: /
suffix: .jsp
实际应用时
<!-- JSTL-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
lombok简化实体类代码的编写工作
常用的方法:getter、setter、toString自动生成lombox的使用要安装插件
实现增删改查(JSP)
1.实体类:User
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private Integer id;
private String name;
}
2.控制器:UserHandler
package com.southwind.controller;
import com.southwind.Service.UserService;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserMapper {
@Autowired
private UserService userService;
@GetMapping("/findall")
public ModelAndView findall(){
ModelAndView modelAndView= new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("list",userService.finAll());
return modelAndView;
}
@GetMapping("findbyid/{id}")
public ModelAndView findById(@PathVariable("id") Integer id){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("update");
modelAndView.addObject("user",userService.findById(id));
return modelAndView;
}
@PostMapping("/save")
public String save(User user){
userService.save(user);
return "redirect:/user/findall";
}
@GetMapping("/delete/{id}")
public String deleteById(@PathVariable("id") Integer id){
userService.delete(id);
return "redirect:/findall";
}
@GetMapping("/update")
public String update( User user){
userService.uodate(user);
return "redirect:/user/findall";
}
}
3.业务Service
接口:
package com.southwind.Service;
import com.southwind.entity.User;
import java.util.Collection;
public interface UserService {
public Collection<User> finAll();
public User findById(Integer id);
public void save(User user);
public void delete(Integer id);
public void uodate(User user);
}
实现类:
package com.southwind.Service.impl;
import com.southwind.Reposity.UserReposity;
import com.southwind.Service.UserService;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserReposity userReposity;
@Override
public Collection<User> finAll() {
return userReposity.finAll();
}
@Override
public User findById(Integer id) {
return userReposity.findById(id);
}
@Override
public void save(User user) {
userReposity.save(user);
}
@Override
public void delete(Integer id) {
userReposity.delete(id);
}
@Override
public void uodate(User user) {
userReposity.uodate(user);
}
}
4.业务:Repositort
接口;
package com.southwind.Reposity;
import com.southwind.entity.User;
import java.util.Collection;
public interface UserReposity {
public Collection<User> finAll();
public User findById(Integer id);
public void save(User user);
public void delete(Integer id);
public void uodate(User user);
}
实现类:
package com.southwind.Reposity.impl;
import com.southwind.Reposity.UserReposity;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class UserReposityImpl implements UserReposity {
private static Map<Integer,User> map;
static {
map=new HashMap<>();
map.put(1,new User(1,"张三"));
map.put(2,new User(2,"李四"));
map.put(3,new User(3,"王五"));
}
@Override
public Collection<User> finAll() {
return map.values();
}
@Override
public User findById(Integer id) {
return map.get(id);
}
@Override
public void save(User user) {
map.put(user.getId(),user);
}
@Override
public void delete(Integer id) {
map.remove(id);
}
@Override
public void uodate(User user) {
map.put(user.getId(),user);
}
}
5.视图层JSP
index.jsp
<%--
Created by IntelliJ IDEA.
User: 郝泾钊
Date: 2022-04-10
Time: 13:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>index</h1>
${mess}
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>
<a href="/user/delete/${user.id}">删除</a>
<a href="/user/findbyid/${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
save.jsp:
<%--
Created by IntelliJ IDEA.
User: 郝泾钊
Date: 2022-04-10
Time: 18:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/user/save" method="post">
<input type="text" name="id"/><br>
<input type="text" name="name"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
update.jsp
<%--
Created by IntelliJ IDEA.
User: 郝泾钊
Date: 2022-04-10
Time: 18:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/user/update" method="post">
<input type="text" name="id" value="${user.id}"/><br>
<input type="text" name="name" value="${user.name}"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
Spring Boot整合JSP --CRUD的更多相关文章
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- Spring boot整合jsp
这几天在集中学习Spring boot+Shiro框架,因为之前view层用jsp比较多,所以想在spring boot中配置jsp,但是spring boot官方不推荐使用jsp,因为jsp相对于一 ...
- 从零开始的Spring Boot(4、Spring Boot整合JSP和Freemarker)
Spring Boot整合JSP和Freemarker 写在前面 从零开始的Spring Boot(3.Spring Boot静态资源和文件上传) https://www.cnblogs.com/ga ...
- Spring Boot学习总结(2)——Spring Boot整合Jsp
怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功 整合jsp,于是决定将整合过程记载下来. 无论使用的是那种 ...
- Spring boot 整合jsp、thymeleaf、freemarker
1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...
- Spring boot 整合jsp和tiles模板
首先贴上我的pox.xml文件,有详细的支持注释说明 <?xml version="1.0" encoding="UTF-8"?> <proj ...
- Spring boot 整合JSP开发步骤
1. 新建Springboot项目,war <dependency> <groupId>org.springframework.boot</groupId> < ...
- 峰哥说技术:09-Spring Boot整合JSP视图
Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 09 峰哥说技术:Spring Boot整合JSP视图 一般来说我们很少推荐大家在Spring boot ...
- Spring Boot 整合视图层技术,application全局配置文件
目录 Spring Boot 整合视图层技术 Spring Boot 整合jsp Spring Boot 整合freemarker Spring Boot 整合视图层技术 Spring Boot 整合 ...
- 从零开始的Spring Boot(5、Spring Boot整合Thymeleaf)
Spring Boot整合Thymeleaf 写在前面 从零开始的Spring Boot(4.Spring Boot整合JSP和Freemarker) https://www.cnblogs.com/ ...
随机推荐
- C#微信公众号关注二维码生成、密文方式
文章说明:是公众号使用自己服务器的处理的其中一篇关注二维码信息处理 1.流程 1.1 需知 全局返回码:这个必须要哦.不然调试的时候接口出的错误怎么处理呢. (闲话:博客的随笔只能添加也给超链呀, ...
- 群晖NAS搭建外网可访问的calibre
一.在群晖docker上安装calibre-web 1. 下载相关的镜像文件 打开Docker后点击左侧注册表,在上方搜索栏搜索calibre 然后我们选择使用 technosoft2000/cali ...
- 关于vlc"编解码器暂不支持: VLC 无法解码格式“MIDI” (MIDI Audio)"解决
解决办法 sudo apt install vlc-plugin-fluidsynth
- 【每日一题】【模拟】2021年11月11日--LRU 缓存机制
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 .实现 LRUCache 类: LRUCache(int capacity) 以正整数作为容量 capacity 初始化 L ...
- 【机器学习】李宏毅——Recurrent Neural Network(循环神经网络)
假设我们当前要做一个人工智能客服系统,那该系统就需要对用户输入的话语进行辨认,例如用户输入: I want to arrive Taipei on November 2nd 那么该系统就能够辨认出来T ...
- Oracle或者Mysql误删表之后的恢复办法
执行drop table 表名;的命令会将表放到回收站里: 执行flashback table 表名 to before drop;的命令就能恢复. 如果忘记删掉了哪个表,可以在数据库工具Navica ...
- 基于K-means聚类算法进行客户人群分析
摘要:在本案例中,我们使用人工智能技术的聚类算法去分析超市购物中心客户的一些基本数据,把客户分成不同的群体,供营销团队参考并相应地制定营销策略. 本文分享自华为云社区<基于K-means聚类算法 ...
- [0x12] 132.小组队列
题意 link(more:UVA540) 简化题意:对 \(n\) 个小组排队,每个小组有至多 \(m\) 个成员(每个成员有唯一编号 \(x\)),当一个人来到队伍时,如果队中有同组成员,直接插入其 ...
- 【ASP.NET Core】按用户等级授权
验证和授权是两个独立但又存在联系的过程.验证是检查访问者的合法性,授权是校验访问者有没有权限查看资源.它们之间的联系--先验证再授权. 贯穿这两过程的是叫 Claim 的东东,可以叫它"声明 ...
- 痞子衡嵌入式:MCUBootUtility v4.0发布,开始支持MCX啦
-- 痞子衡维护的 NXP-MCUBootUtility 工具距离上一个大版本(v3.5.0)发布过去 9 个月了,这一次痞子衡为大家带来了版本升级 v4.0.0,这个版本主要有两个重要更新需要跟大家 ...