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. 华为云平台部署教程之CNA\VRM的安装

    本教程仅含华为云平台搭建部署中CNA和VRM的安装,请按需求选择查看本文. 一.前期准备 1.硬件 服务器*4 交换机*3 网线 个人PC机 2.软件 PC机系统(win7/win10) KVM软件 ...

  2. C++一个吃豆人小游戏

    C++一个吃豆人小游戏 代码如下 #include <cstdio>#include <iostream>#include <ctime>#include < ...

  3. Multipass,本地轻量级Linux体验!

    Multipass介绍 Multipass 是由Ubuntu官方提供,在Linux,MacOS和Windows上快速生成 Ubuntu虚拟机 的工具.它提供了一个简单但功能强大的CLI,可让我们在本地 ...

  4. MICCAI 论文投稿须知翻译

    MICCAI 论文投稿须知翻译 以MICCAI 2021 PAPER SUBMISSION AND REBUTTAL GUIDELINES为例,每年投稿须知类似 作者信息和rebuttal 本文件包含 ...

  5. HCIE Routing&Switching之MPLS静态LSP配置

    前文我们了解了MPLS基础理论部分,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16928096.html:今天我们来聊一聊MPLS静态LSP配置相关话题: ...

  6. 【中间件】K8S-kubernetes

    一.概念 1.为什么使用k8s 生产型应用会涉及多个容器.这些容器必须跨多个服务器主机进行部署 可以构建跨多个容器的应用服务.跨集群调度.扩展这些容器,并长期持续管理这些容器的健康状况 在Docker ...

  7. 基于Nginx搭建WebDAV服务

    title: 基于Nginx搭建WebDAV服务 categories: - [IT,网络,服务] tags: - WebDAV - Nginx comments: true date: 2022-1 ...

  8. Window系统的mysql数据库定时备份

    原文:Window系统的mysql数据库定时备份 - Stars-One的杂货小窝 最近老大提到了数据库备份的功能,由于服务器是window系统的,所以研究了下备份的方案,特此记录 主要是实现每天定时 ...

  9. linux安装influxdb和chronograf

    安装环境: Alibaba Cloud Linux 安装influxdb 1.下载.安装 wget https://dl.influxdata.com/influxdb/releases/influx ...

  10. 链接脚本中 AT> 的作用

    缘由 这两天在梳理芯片的启动流程, 研究到了链接脚本(样本为stm32cube ide 中的 stm32f407 的 flash 链接脚本). 产生了一个疑问AT>是什么? 答案 AT>就 ...