SpringBoot:web开发

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.mfbz.cn/a/376929.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2024.02 国内认知大模型汇总

概述 大模型&#xff0c;又称为大规模机器学习模型&#xff0c;是一种基于大数据的人工智能技术。它通过深度学习和机器学习的方法&#xff0c;对大量数据进行训练&#xff0c;以实现对复杂问题的高效解决。大模型技术在语音识别、图像识别、自然语言处理等领域有着广泛的应用…

常用对象和常用成员函数

常量对象与常量成员函数来防止修改对象&#xff0c;实现最低权限原则。 在Obj被定义为常量对象的情况下&#xff0c;下面这条语句是错误的。 错误的原因是常量对象一旦初始化后&#xff0c;其值就再也不能改变。因此&#xff0c;不能通过常量对象调用普通成员函数&#xff0c;因…

ArcGIS学习(五)坐标系-2

3.不同基准面坐标系之间的转换 在上一关中,我们学习了ArcGIS中的投影(投影栅格)工具,并以"WGS1984地理坐标系与WGS1984的UTM投影坐标系的转换”为例进行讲解。 "WGS1984地理坐标系与WGS1984的UTM投影坐标系的转换”代表的是同一个基准面下的两个坐标的转换。 …

【力扣】查找总价格为目标值的两个商品,双指针法

查找总价格为目标值的两个商品原题地址 方法一&#xff1a;双指针 这道题和力扣第一题“两数之和”非常像&#xff0c;区别是这道题已经把数组排好序了&#xff0c;所以不考虑暴力枚举和哈希集合的方法&#xff0c;而是利用单调性&#xff0c;使用双指针求解。 考虑数组pric…

洛希极限

L1-3 洛希极限 分数 10 作者 陈越 单位 浙江大学 科幻电影《流浪地球》中一个重要的情节是地球距离木星太近时&#xff0c;大气开始被木星吸走&#xff0c;而随着不断接近地木“…

【数据结构】链表OJ面试题2《分割小于x并排序链表、回文结构、相交链表》+解析

1.前言 前五题在这http://t.csdnimg.cn/UeggB 休息一天&#xff0c;今天继续刷题&#xff01; 2.OJ题目训练 1. 编写代码&#xff0c;以给定值x为基准将链表分割成两部分&#xff0c;所有小于x的结点排在大于或等于x的结点之前 。链表分割_牛客题霸_牛客网 思路 既然涉及…

春节回家坐飞机后助听器就不好用了?如何过安检、拖运?

春节即将来临&#xff0c;很多人都要乘坐飞机回家或者出游。如果你是一位助听器使用者&#xff0c;你可能会有一些疑问&#xff1a;坐飞机能戴助听器吗&#xff1f;助听器会不会受到安检设备的影响&#xff1f;直接将助听器放在传送带上可以吗&#xff1f;……别担心&#xff0…

Harbor介绍、整体架构和安装

Harbor介绍、整体架构和安装 文章目录 Harbor介绍、整体架构和安装1.Harbor介绍2.Harbor 整体架构3.安装Harbor3.1 主机初始化3.1.1 设置ip地址3.1.2 配置镜像源3.1.3 关闭防火墙3.1.4 禁用SELinux3.1.5 禁用swap3.1.6 设置时区 3.2 安装docker3.3 安装docker compose3.4 下载H…

【JS逆向一】逆向某站的 加密参数算法--仅供学习参考

逆向日期&#xff1a;2024.02.06 使用工具&#xff1a;Node.js 文章全程已做去敏处理&#xff01;&#xff01;&#xff01; 【需要做的可联系我】 可使用AES进行解密处理&#xff08;直接解密即可&#xff09;&#xff1a;在线AES加解密工具 1、打开某某网站(请使用文章开头的…

记录 | linux下切换python版本

查看系统中存在的 python 版本 ls /usr/bin/python* 查看系统默认的 python 版本 python --version

金融信贷风控特征计算详解

特征的含义&#xff1f; 特征可以说是风控系统中的最小单元&#xff0c;是风控工具的重要组成部分&#xff0c;我们也可以理解成变量。不过叫什么问题不大&#xff0c;团队内有相同的共识就行。 风控特征是我们做数字化线上风控中的重要组成部分&#xff0c;几乎可以说没有风…

【Flink入门修炼】1-2 Mac 搭建 Flink 源码阅读环境

在后面学习 Flink 相关知识时&#xff0c;会深入源码探究其实现机制。因此&#xff0c;需要现在本地配置好源码阅读环境。 本文搭建环境&#xff1a; Mac M1&#xff08;Apple Silicon&#xff09;Java 8IDEAFlink 官方源码 一、 下载 Flink 源码 github 地址&#xff1a;h…

[每周一更]-(第85期):NLP-实战操作-文本分类

NLP文本分类的应用场景 医疗领域 - 病历自动摘要&#xff1a; 应用&#xff1a; 利用NLP技术从医疗文档中自动生成病历摘要&#xff0c;以帮助医生更快速地了解患者的状况。 法律领域 - 法律文件分类&#xff1a; 应用&#xff1a; 使用文本分类技术自动分类法律文件&#xf…

Mysql的sql优化

一.查询优化 我们都知道&#xff0c;在建立索引的时候&#xff0c;要考虑where后面的查询条件字段、order by 排序后面的字段 、group by 分组排序后面的字段&#xff0c;对他们的字段建立合适的索引&#xff0c;但是我们需要思考怎么建立合适的索引&#xff0c;或者建立索引之…

计算机网络-华为无线网络配置

前面已经大致了解了无线通信的原理和无线组网的概念&#xff0c;今天来学习无线的配置过程与步骤。 一、无线组网配置流程 在开始配置前复习下前面讲过无线组网有涉及几个设备&#xff0c;AC无线控制器、AP无线接入点、POE交换机。无线组网与有线组网是相对独立的&#xff0c;不…

10:LED点阵显示汉字

LED点阵显示汉字 1、字模2、横向取模 1、字模 (1)如何记录组成字的LED点阵亮灭信息&#xff08;16x16点阵一共有256点&#xff0c;显示一个特定的字需要其中有些点亮而另一些不亮&#xff0c;如何记录哪些点亮哪些点不亮&#xff1f;用字模)字模如何工作&#xff1f;256个点用…

机器学习 | 揭示EM算法和马尔可夫链的实际应用

目录 初识EM算法 马尔可夫链 HMM模型基础 HMM模型使用 初识EM算法 EM算法是一种求解含有隐变量的概率模型参数的迭代算法。该算法通过交替进行两个步骤&#xff1a;E步骤和M步骤&#xff0c;从而不断逼近模型的最优参数值。EM算法也称期望最大化算法&#xff0c;它是一个基…

负重20kg复合翼垂直起降无人机应用,复合翼无人机技术分析

主要任务应用 1.管线巡查 挂载可见光/红外二合一光电载荷和小型SAR设备&#xff0c;对既定线路进行昼夜巡视侦察&#xff0c;利用图像实时传回指挥控制中心&#xff0c;可用于石油管路、电力线路、舰艇航线及周围态势感知&#xff0c;利于依据现场实情进行战略决策和指令传达…

车载网络测试 - 总线基础 - CAN总线负载计算

我想做过CAN总线测试的都有遇到过拉高总线负载相关的测试&#xff0c;这个时候我们一般都会通过增加报文的数量或者减小报文的周期来实现&#xff0c;但是CAN总线上的负载到底是如何计算的呢&#xff1f;我想很多人都会有这个疑问吧&#xff0c;那么今天我们一起来看下如何计算…
最新文章