SpringBoot:web开发

news2024/10/6 6:03:49

web开发demo:点击查看 LearnSpringBoot05Web

点击查看更多的SpringBoot教程

技术摘要

  1. webjars
  2. Bootstrap
  3. 模板引擎thymeleaf
  4. 嵌入式Servlet容器
  5. 注册web三大组件

一、webjars

webjars官网
简介
在这里插入图片描述

简介翻译
WebJars 是打包到 JAR(Java Archive)文件中的客户端 Web 库(例如 jQuery 和 Bootstrap)。 显式且轻松地管理基于 JVM 的 Web 应用程序中的客户端依赖项 使用基于 JVM 的构建工具(例如 Maven、Gradle、sbt…)下载客户端依赖项 了解您正在使用哪些客户端依赖项 传递依赖项会自动解析并通过 RequireJS 选择性加载 部署在 Maven 中心 公共 CDN。

pom.xml添加webjars依赖
在这里插入图片描述

pom.xml添加webjars依赖代码

    <!--
            https://www.webjars.org/

            WebJars介绍 https://blog.csdn.net/Adrian_Dai/article/details/80505076

            http://localhost:8084/webjars/jquery/3.6.4/jquery.js  访问webjars的jquery静态资源

            注意:http://localhost:8084/crud访问地址后面加/crud,
            因为在application.properties里配置了访问路径: server.servlet.context-path=/crud
            http://localhost:8084/crud/webjars/jquery/3.6.4/jquery.js  访问webjars的jquery静态资源
          -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.4</version>
        </dependency>

        <!--
        https://www.webjars.org/ 里找到 bootstrap

        bootstrap官网:https://getbootstrap.com/
        bootstrap中文网:https://www.bootcss.com/
        https://github.com/twbs/bootstrap

        http://localhost:8084/webjars/bootstrap/4.0.0/js/bootstrap.js  访问webjars的bootstrap静态资源

            注意:http://localhost:8084/crud访问地址后面加/crud,
            因为在application.properties里配置了访问路径: server.servlet.context-path=/crud
            http://localhost:8084/crud/webjars/bootstrap/4.0.0/js/bootstrap.js  访问webjars的bootstrap静态资源

        -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
<!--            <version>5.3.1</version>-->
            <version>4.0.0</version>
        </dependency>

项目启动后,访问webjars的jquery的jquery.js静态资源
在这里插入图片描述

项目启动后,访问webjars的bootstrap的js/bootstrap.js静态资源
在这里插入图片描述

二、Bootstrap

Bootstrap官网
Bootstrap中文网
Github里的Bootstrap
Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML、CSS、JavaScript 开发的简洁、直观、强悍的前端开发框架,使得 Web 开发更加快捷。Bootstrap提供了优雅的HTML和CSS规范,它即是由动态CSS语言Less写成。

三、模板引擎thymeleaf

thymeleaf官网
Github里thymeleaf
简介
在这里插入图片描述

简介翻译
Thymeleaf是一个现代的服务器端Java模板引擎,适用于web和独立环境。
Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板——HTML可以在浏览器中正确显示,也可以作为静态原型工作,允许开发团队之间更强的协作。
有了Spring框架的模块,与你最喜欢的工具的大量集成,以及插入你自己的功能的能力,Thymeleaf是现代HTML5 JVM web开发的理想选择——尽管它还有更多的功能。

添加spring-boot-starter-thymeleaf依赖
Spring-Boot的starters文档找到spring-boot-starter-thymeleaf在这里插入图片描述

在这里插入图片描述

pom.xml添加spring-boot-starter-thymeleaf依赖代码

        <!--
            spring-boot-starter-thymeleaf
            https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.starters
            https://github.com/thymeleaf/thymeleaf

            TemplateEngineConfigurations
            org.springframework.boot.autoconfigure.thymeleaf


            把html页面放在 classpath:/templates/ ,thymeleaf就可以自动渲染了
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

把html页面放在 classpath:/templates/ ,thymeleaf就可以自动渲染了
在这里插入图片描述

四、嵌入式Servlet容器

MyServeltConfig.java类里配置嵌入式的servlet容器在这里插入图片描述

查看支持嵌入其他servlet
在这里插入图片描述

MyServeltConfig.java代码

package com.example.learnspringboot05web.config;

import com.example.learnspringboot05web.filter.MyFilter;
import com.example.learnspringboot05web.listener.Mylistener;
import com.example.learnspringboot05web.servlet.Myservlet;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.management.MemoryUsage;
import java.lang.reflect.Array;
import java.util.Arrays;

@Configuration
public class MyServeltConfig {

    //注册三大组件
    @Bean
    public ServletRegistrationBean myServlet() {
        //http://localhost:8084/crud/myServlet
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new Myservlet(), "/myServlet");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean mylistener(){
        ServletListenerRegistrationBean<Mylistener> registrationBean = new ServletListenerRegistrationBean<Mylistener>(new Mylistener());
        return registrationBean;
    }


    //配置嵌入式的servlet容器

    /*

        https://www.jianshu.com/p/b973476ccfd6   https://blog.csdn.net/qq_43843951/article/details/108049897
        Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代

     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> webServerFactoryWebServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>() {
            //定制嵌入式的servlet容器相关规则

            @Override
            public void customize(ConfigurableJettyWebServerFactory factory) {
                factory.setPort(8082);
            }
        };
    }
}

五、注册web三大组件

web三大组件分别是:

  1. Servlet
  2. Filter: 过滤器
  3. Listener :监听器

对应的Bean类

  1. ServletRegistrationBean
  2. FilterRegistrationBean
  3. ServletListenerRegistrationBean

在MyServeltConfig.java类里注册三大组件
在这里插入图片描述

Myservlet.java代码

package com.example.learnspringboot05web.servlet;

import com.sun.source.util.DocSourcePositions;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class Myservlet extends HttpServlet {
    //http://localhost:8084/crud/myServlet
    //处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello Myservlet");
    }
}

Mylistener.java代码

package com.example.learnspringboot05web.listener;

import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;

public class Mylistener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Mylistener 执行了 contextInitialized web应用启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Mylistener 执行了 contextDestroyed web项目销毁");

    }
}

MyFilter.java代码

package com.example.learnspringboot05web.filter;

import jakarta.servlet.*;

import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("MyFilter 执行了 doFilter ");
        chain.doFilter(request, response );
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

Mylistener组件监听结果
在这里插入图片描述

Servlet和MyFilter组件回调
项目启动后,在浏览器地址栏访问:http://localhost:8084/crud/myServlet
在这里插入图片描述
web获取到Servlet组件写入的数据在这里插入图片描述
MyFilter组件回调在这里插入图片描述

六、bootstrap和thymeleaf配合使用的效果

项目启动后,在浏览器地址栏里访问http://localhost:8084/crud/
账户:admin;密码:123456
在这里插入图片描述

登录成功进入主页
在这里插入图片描述

员工管理页面
在这里插入图片描述

编辑页面
在这里插入图片描述

添加员工页面
在这里插入图片描述

application.properties代码


#查看 ServerProperties 源码

server.port=8084
#spring.web.resources.static-locations=classpath:/hello, classpath:/test

# 禁止混存, 修改后 按住 commd + f9 重新编译
spring.thymeleaf.cache=false

# 访问路径必须是 crud ? http://localhost:8084/crud/
server.servlet.context-path=/crud
# 绑定国际化 从 i18n文件里读取
spring.messages.basename=i18n.login

#默认的格式:dd/MM/yyyy
spring.mvc.format.date=yyyy-MM-dd

#SpringBoot-RuntimeException缺少exception和message问题解决方法,  这个问题是由于SpringBoot2.0以上版本,需要在配置文件中指定server的异常对象和异常信息
# https://blog.csdn.net/qq_40898909/article/details/126346500     https://blog.csdn.net/qq_33879627/article/details/106621563
# 查看 ErrorProperties 源码 private IncludeAttribute includeMessage = IncludeAttribute.NEVER;  includeException 默认 false
server.error.include-exception=true
server.error.include-message=always

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>3.1.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>LearnSpringBoot05Web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>LearnSpringBoot05Web</name>
    <description>LearnSpringBoot05Web</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--
        引入其他servlet

        jetty启动java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext
        解决方法:pom文件中添加依赖

        jakarta.servlet
        jakarta.servlet-api
        5.0.0

        原因:此时的jetty是11.x版本的,不支持jakarta.servlet-api 6,改成5即可
        原文链接:https://blog.csdn.net/weixin_44866139/article/details/132006996
        -->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.springframework.boot</groupId>-->
<!--                    <artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
<!--        </dependency>-->

<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-undertow</artifactId>-->
<!--&lt;!&ndash;            <artifactId>spring-boot-starter-jetty</artifactId>&ndash;&gt;-->
<!--                                    undertow  和 jetty 二选一                   -->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--
            https://www.webjars.org/

            WebJars介绍 https://blog.csdn.net/Adrian_Dai/article/details/80505076

            http://localhost:8084/webjars/jquery/3.6.4/jquery.js  访问webjars的jquery静态资源

            注意:http://localhost:8084/crud访问地址后面加/crud,
            因为在application.properties里配置了访问路径: server.servlet.context-path=/crud
            http://localhost:8084/crud/webjars/jquery/3.6.4/jquery.js  访问webjars的jquery静态资源
          -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.4</version>
        </dependency>

        <!--
        https://www.webjars.org/ 里找到 bootstrap

        bootstrap官网:https://getbootstrap.com/
        bootstrap中文网:https://www.bootcss.com/
        https://github.com/twbs/bootstrap

        http://localhost:8084/webjars/bootstrap/4.0.0/js/bootstrap.js  访问webjars的bootstrap静态资源

            注意:http://localhost:8084/crud访问地址后面加/crud,
            因为在application.properties里配置了访问路径: server.servlet.context-path=/crud
            http://localhost:8084/crud/webjars/bootstrap/4.0.0/js/bootstrap.js  访问webjars的bootstrap静态资源

        -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
<!--            <version>5.3.1</version>-->
            <version>4.0.0</version>
        </dependency>

        <!--
            spring-boot-starter-thymeleaf
            https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.starters
            https://github.com/thymeleaf/thymeleaf

            TemplateEngineConfigurations
            org.springframework.boot.autoconfigure.thymeleaf


            把html页面放在 classpath:/templates/ ,thymeleaf就可以自动渲染了
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1438382.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java线程是怎么实现run方法的执行的呢?【 多线程在JVM中的实现原理剖析】

Java线程是怎么实现run方法的执行的呢&#xff1f;【 多线程在JVM中的实现原理剖析】 查看naive state0 方法JVM_StartThread 方法创建操作系统线程操作系统线程执行 本文转载-极客时间 我们知道Java线程是通过行start()方法来启动的&#xff0c;线程启动后会执行run方法内的代…

【Spring基础】从0开始学习Spring(2)

前言 在上篇文章&#xff0c;我已经讲了Spring中最核心的知识点&#xff1a;IoC&#xff08;控制反转&#xff09;以及DI&#xff08;依赖注入&#xff09;。这篇文章&#xff0c;我将讲一下关于Spring框架中的其它比较琐碎但是又还是挺重要的知识点&#xff0c;因此&#xff…

Excel——有效性、二级菜单联动

一、录入规范数据 1.手动输入序列录入有效性信息 选择需要录入有效性的所有单元格 选择【数据】——【有效性】——【有效性】 在【允许】输入的值之间选择【序列】 在【序列】输入框中输入想要选择的值&#xff0c;中间用逗号&#xff08;必须是英文逗号&#xff09;隔开 。…

[C/C++] -- Boost库、Muduo库编译安装使用

1.Muduo库 Muduo 是一个基于 C11 的高性能网络库&#xff0c;其核心是事件驱动、非阻塞 I/O、线程池等技术&#xff0c;以实现高并发、高性能的网络通信。Muduo 库主要由陈硕先生开发维护&#xff0c;已经成为 C 服务器程序员的常用工具之一。 Muduo 库的主要特点&#xff1a…

NLP_循环神经网络(RNN)

文章目录 RNN结构RNN实战RNN小结 RNN结构 NPLM 在处理长序列时会面临一些挑战。首先&#xff0c;由于它仍然是基于词的模型&#xff0c;因此在处理稀有词汇或者词汇表外的词汇时效果不佳。其次&#xff0c;NPLM不能很好地处理长距离依赖关系。而上面这两个局限&#xff0c;恰恰…

Python进阶--爬取下载人生格言(基于格言网的Python3爬虫)

目录 一、此处需要安装第三方库: 二、抓包分析及Python代码 1、打开人生格言网&#xff08;人生格言-人生格言大全_格言网&#xff09;进行抓包分析 2、请求模块的代码 3、抓包分析人生格言界面 4、获取各种类型的人生格言链接 5、获取下一页的链接 6、获取人生格言的…

基于Java (spring-boot)的酒店管理系统

一、项目介绍 本系统的使用者一共有酒店管理员和用户这两种角色: 1、酒店管理员功能&#xff1a; 登录&#xff1a;管理员可以通过登录功能进入系统&#xff0c;确保只有授权人员可以访问系统。 用户管理&#xff1a;管理员可以添加、编辑和删除酒店的用户&#xff0c;包括前…

【React】如何使antd禁用状态的表单输入组件响应点击事件?

最近遇到一个需求&#xff0c;需要在<Input.textarea>组件中&#xff0c;设置属性disabled为true&#xff0c;使textarea响应点击事件&#xff0c;但直接绑定onClick并不会在禁用状态下被响应。 解决方法1 之后尝试了很多方法&#xff0c;比如设置csspointer-events:no…

python30-Python的运算符结合性和优先级

1&#xff09;所有的数学运算都是从左向右进行的&#xff0c;Python 语言中的大部分运算符也是从左向右结合的&#xff0c;只有单目运算符、赋值运算符和三目运算符例外&#xff0c;它们是从右向左结合的&#xff0c;也就是说&#xff0c;它们是从右向左运算的。 2&#xff09…

01动力云客之环境准备+前端Vite搭建VUE项目入门+引入Element PLUS

1. 技术选型 前端&#xff1a;Html、CSS、JavaScript、Vue、Axios、Element Plus 后端&#xff1a;Spring Boot、Spring Security、MyBatis、MySQL、Redis 相关组件&#xff1a;HiKariCP&#xff08;Spring Boot默认数据库连接池&#xff09;、Spring-Data-Redis&#xff08;S…

JVM Java虚拟机入门指南

文章目录 为什么学习JVMJVM的执行流程JVM的组成部分类加载运行时数据区本地方法接口执行引擎 垃圾回收什么样的对象是垃圾呢内存溢出和内存泄漏定位垃圾的方法对象的finalization机制垃圾回收算法分代回收垃圾回收器 JVM调优参数JVM调优工具Java内存泄漏排查思路CPU飙高排查方案…

力扣精选算法100道—— 连续数组(前缀和专题)

连续数组&#xff08;前缀和专题&#xff09; 目录 &#x1f6a9;了解题意 &#x1f6a9;算法原理 ❗为什么hash设置成<0,-1>键值对 ❗与和为K的子数组比较hash的键值对 &#x1f6a9;代码实现 &#x1f6a9;了解题意 我们看到给定数组里面只有0和1&#xff0c;我们…

【JMeter】使用技巧

在这此对新版本jmeter的学习温习的过程&#xff0c;发现了一些以前不知道的功能&#xff0c;所以&#xff0c;整理出来与大分享。本文内容如下。 如何使用英文界面的jmeter如何使用镜像服务器Jmeter分布式测试启动Debug 日志记录搜索功能线程之间传递变量 如何使用英文界面的…

VMware虚拟机清理瘦身

用了一段时间VMware虚拟机之后&#xff0c;发现内存越来越小&#xff0c;也没装什么软件。。。 1.查询磁盘空间分布 虚拟机中磁盘空间查询 先看一下哪些地方占用的空间大&#xff0c;进行排查。 2.排查VMware复制文件产生的缓存路径 VMware复制文件有一个特点&#xff0c;以…

4、ChatGPT 无法完成的 5 项编码任务

ChatGPT 无法完成的 5 项编码任务 这是 ChatGPT 不能做的事情的一个清单,但这并非详尽无遗。ChatGPT 可以从头开始生成相当不错的代码,但是它不能取代你的工作。 我喜欢将 ChatGPT 视为 StackOverflow 的更智能版本。非常有帮助,但不会很快取代专业人士。当 ChatGPT 问世时…

docker常用10条容器操作命令

Docker 中一些常用的容器操作命令&#xff0c;我们可以根据需要使用这些命令来管理和操作 Docker 容器。我们这次以Hell-world这个镜像为例来说明&#xff1a; 1. docker pull hello-world #拉取hell-world镜像 2. docker images # 查看本地拉取的镜像 3. docker run hello…

VM 虚拟机和容器技术之间有什么区别?

随着云计算技术的不断发展&#xff0c;虚拟机和容器技术作为两种常见的虚拟化技术&#xff0c;被广泛应用于云计算领域。虽然虚拟机和容器技术都是虚拟化技术&#xff0c;但它们之间存在一些重要的区别。本文将详细介绍虚拟机和容器技术的区别&#xff0c;以便读者更好地了解这…

Qt信号和槽机制(什么是信号和槽,connect函数的形式,按钮的常用信号,QWidget的常用槽,自定义槽函数案例 点击按钮,输出文本)

一.什么是信号和槽 信号槽式Qt中的一个很重要的机制。信号槽实际上是观察者模式,当发生了感兴趣的事件&#xff0c;某一个操作就会被自动触发。当某个事件发生之后&#xff0c;比如按钮检测到自己被点击了一下&#xff0c;它就会发出一个信号。这种发出类似广播。如果有对象对…

HubSpot x 小红书:MessageBox打破数据壁垒

在当今数字营销的快速发展环境中&#xff0c;企业面临着将多个系统平台整合在一起以实现更有效营销策略的挑战。然而&#xff0c;随着技术的不断进步&#xff0c;诸如MessageBox这样的工具正在成为解决这一挑战的关键。MessageBox作为一种能够对接多个系统平台的工具&#xff0…

进程间通信(4):消息队列

先进先出&#xff0c;保证信息的有序性。 函数&#xff1a;msgget(搭配ftok)、msgsnd、msgrcv、msgctl 实现流程&#xff1a; 1、创建消息队列IPC对象 msgget 2、通信(内置函数&#xff1a;msgsnd、msgrcv) 3、删除消息队列IPC对象 msgctl write.c /* * 文件名称&…