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的更多相关文章

  1. spring boot整合jsp的那些坑(spring boot 学习笔记之三)

    Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency>            <groupId>or ...

  2. Spring boot整合jsp

    这几天在集中学习Spring boot+Shiro框架,因为之前view层用jsp比较多,所以想在spring boot中配置jsp,但是spring boot官方不推荐使用jsp,因为jsp相对于一 ...

  3. 从零开始的Spring Boot(4、Spring Boot整合JSP和Freemarker)

    Spring Boot整合JSP和Freemarker 写在前面 从零开始的Spring Boot(3.Spring Boot静态资源和文件上传) https://www.cnblogs.com/ga ...

  4. Spring Boot学习总结(2)——Spring Boot整合Jsp

    怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功 整合jsp,于是决定将整合过程记载下来. 无论使用的是那种 ...

  5. Spring boot 整合jsp、thymeleaf、freemarker

    1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...

  6. Spring boot 整合jsp和tiles模板

    首先贴上我的pox.xml文件,有详细的支持注释说明 <?xml version="1.0" encoding="UTF-8"?> <proj ...

  7. Spring boot 整合JSP开发步骤

    1. 新建Springboot项目,war <dependency> <groupId>org.springframework.boot</groupId> < ...

  8. 峰哥说技术:09-Spring Boot整合JSP视图

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 09  峰哥说技术:Spring Boot整合JSP视图 一般来说我们很少推荐大家在Spring boot ...

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

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

  10. 从零开始的Spring Boot(5、Spring Boot整合Thymeleaf)

    Spring Boot整合Thymeleaf 写在前面 从零开始的Spring Boot(4.Spring Boot整合JSP和Freemarker) https://www.cnblogs.com/ ...

随机推荐

  1. 解决Anaconda关联VSCode使用conda运行Python报错(无法将“conda”项识别为 cmdlet、函数、脚本文件或可运行程序)

    错误 刚安装好Anaconda之后创建好VS Code环境运行Python会报错,但是仍然是可以正常运行,强迫症想解决报错 PS C:\Users\Satan\Documents\Code\Pytho ...

  2. Selenium4+Python3系列(八) - Cookie、截图、单选框及复选框处理、富文本框、日历控件操作

    我所在的城市昨天出了近20+的阳性案例,但这丝毫没有 "影响" 到996的工作时间,当然,也没有影响到我想继续更新文章的决心. 一.cookie常用操作入门 上一篇有写过关于coo ...

  3. 165 pbi-utils 使用文档

    165 pbi-utils 使用文档 一.背景 先来说一下为什么会有 pbi-utils 这个小工具吧.在我日常做演示的示例文件的时候,每次都要重新搞一次 Power BI Desktop,就想能不能 ...

  4. 这可能是最全的SpringBoot3新版本变化了!

    11月24号,Spring Boot 3.0 发布了第一个正式的 GA 版本,一起看看新版本到底有哪些变化. 2.7版本升级指南 官方提供了一个从 2.7 版本升级到 3.0 的指南:https:// ...

  5. 2020最新Java面试题及答案(带完整目录).pdf

    一.JVM 二.Java集合 三.Java多线程并发 四.Java基础 五.Spring原理 六.微服务 七.Netty与RPC 八.网络 九.日志 十.RabbitMQ 十一.MongoDB 十二. ...

  6. 3.7V升压5V,3.7V转5V电路图芯片

    锂离子电池在如今是广泛应用存在我们生活中的方方面面的电子产品中.如,电子玩具,美容仪,医疗产品,智能手表,手机,笔记本,电动汽车等等非常多. 锂电池3.7V升压到5V,3.7V转5V稳压输出的电子产品 ...

  7. 看起来简单实际上却很牛的KMP算法:LeetCode572-另一棵树的子树

    题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 暴力解法 从 ...

  8. IdentityServer4的最佳使用

    简介   本人做微服务项目也有一段时间了,在微服务中让我感触颇深的就是这个IdentityServer4了,ID4(IdentityServer4的简称)中涉及的概念颇多,本文不谈概念(我怕读者没耐心 ...

  9. 搭建漏洞环境及实战——搭建DVWA漏洞环境

    DVWA是一款开源的渗透测试漏洞练习平台,其中内涵XSS.SQL注入.文件上传.文件包含.CSRF和暴力破解等各个难度的测试环境. 1.在安装时需要在数据库里创建一个数据库名,进入MySQL管理中的p ...

  10. echarts去除下载小图标

    toolbox: { show: true, orient: 'vertical', left: 'right', top: 'center', feature: { dataView: { read ...