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. 【Java 并发003】原理层面:Java并发三特性全解析

    一.前言 不管什么语言,并发的编程都是在高级的部分,因为并发的涉及的知识太广,不单单是操作系统的知识,还有计算机的组成的知识等等.说到底,这些年硬件的不断的发展,但是一直有一个核心的矛盾在:CPU.内 ...

  2. [排序算法] 直接/折半插入排序 (C++)

    插入排序解释 插入排序很好理解,其步骤是 :先将第一个数据元素看作是一个有序序列,后面的 n-1 个数据元素看作是未排序序列.对后面未排序序列中的第一个数据元素在这个有序序列中进行从后往前扫描,找到合 ...

  3. UBOOT编译--- UBOOT全部目标的编译过程详解(九)

    1. 前言 UBOOT版本:uboot2018.03,开发板myimx8mmek240. 2. 概述 本文接续上篇文章,采用自下而上的方法,先从最原始的依赖开始,一步一步,执行命令生成目标.这里先把上 ...

  4. 2流高手速成记(之九):基于SpringCloudGateway实现服务网关功能

    咱们接上回 上一节我们基于Sentinel实现了微服务体系下的限流和熔断,使得整个微服务架构的安全性和稳定性上升了一个台阶 篇尾我们引出了一个问题,众多的微服务节点,我们如何部署才能满足客户端简洁高效 ...

  5. PEP8语法规范解释说明

    PEP8规范解析 内容概要: 1.PEP8规范是什么? 2.PEP8相关内容 1.PEP8规范是什么 PEP是Python Enhancement Proposal的缩写,翻译为:"Pyth ...

  6. Velocity模板引擎的的使用示例(入门级)

    简单说下这个引擎的两个分支(虽然语言不同调用方法大同小异): 1.Java平台下的:org.apache.velocity 2..Net平台下的:NVelocity 注:本文章不涉及到后端只说模板的使 ...

  7. 网络I/O模型 解读

    网络.内核 网卡能「接收所有在网络上传输的信号」,但正常情况下只接受发送到该电脑的帧和广播帧,将其余的帧丢弃. 所以网络 I/O 其实是网络与服务端(电脑内存)之间的输入与输出 内核 查看内核版本 : ...

  8. 关于Mybatis-Plus中update()、updateById()方法的使用及null值的判断

    使用场景说明: 在 Mybatis-Plus 的使用过程中,经常会遇对数据库更新的情况 更新常用方法:update().updateById() 问题:经常会遇见对 null 值的处理,对传入的实体参 ...

  9. [数学建模]层次分析法AHP

    评价类问题. 问题: ① 评价的目标? ② 可选的方案? ③ 评价的指标? 分层 目标层.准则层.方案层 层次分析法可分为四个步骤建立: 第一步:标度确定和构造判断矩阵: 第二步:特征向量,特征根计算 ...

  10. 为文本框控件添加滚动条-CEdit

    在VS2015环境下操作 创建文本框控件 设置控件属性 效果