IDEA2023 SpringBoot整合Web开发(二)

一、SpringBoot介绍

由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。SpringBoot提供了一种新的编程范式,可以更加快速便捷地开发Spring项目,在开发过程当中可以专注于应用程序本身的功能开发,而无需在Spring配置上花太大的工夫。

SpringBoot基于Spring4进行设计,继承了原有Spring框架的优秀基因。SpringBoot准确的说并不是一个框架,而是一些类库的集合。maven或者gradle项目导入相应依赖即可使用 SpringBoot,而无需自行管理这些类库的版本。

二、SpringBoot特点:

  1. 自动配置:SpringBoot提供自动配置功能,根据项目的依赖和环境自动设置 Spring应用程序,减少了手动配置的复杂度。
  2. 启动器:SpringBoot提供"启动器"依赖集合,如: spring-boot-starter-web 简化了项目的依赖管理。
  3. 嵌入式服务器:SpringBoot支持嵌入式服务器,如Tomcat、Jetty和Undertow,使得应用程序可以独立运行,无需外部Web服务器。
  4. 生产级别的特性:SpringBoot具备生产级别的功能,包括健康检查、应用监控、日志管理等。Actuator 模块可以轻松监控和管理应用程序。
  5. 无配置的约定:SpringBoot遵循"无配置"的原则,使用合理的默认值和约定,减少需要编写的配置代码。
  6. 快速开发:SpringBoot的项目结构和默认配置帮助开发者快速启动新项目。内置工具和插件支持开发、测试和部署。

三、Springboot3 版本要求

四、SpringBoot的项目结构

五、SpringBoot整合Web开发_静态资源

SpringBoot项目中没有WebApp目录,只有src目录。在src/main/resources下面有static和templates两个文件夹。SpringBoot默认在static目录中存放静态资源,而在templates中放动态页面。

index.html 页面

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
    <!--脚本-->
    <script src="js/index.js"></script>
    <!--样式-->
    <link rel="stylesheet" href="css/index.css"></link>
</head>
<body>
   <div id="app">
       <!--图片-->
    <img src="img/1.png" alt="butterfly"/><p/>
       <button onclick="my()">单击我</button>
   </div>

</body>
</html>

index.css

css 复制代码
#app{
    background: lightcyan;
    height: 100vh;
    width: auto;
    display: flex;
    justify-content: center;
    align-items: center;
}

button{
    width: 100px;
    height: 150px;
}

index.js

javascript 复制代码
function my(){
    alert("测试JS!");
}

运行:

六、SpringBoot整合Web开发Servlet

SpringBoot项目没有web.xml文件,所以无法在web.xml中注册web组件,SpringBoot有自己的方式注册web组件。

1、创建MyLoginServlet类

java 复制代码
package com.hlx.springbootdemo1;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author : HLX
 * @ClassName :MyLoginServlet
 * @date : 2024/11/19 15:34
 * @Version :1.0
 * @Description: TODO
 * @modyified By :
 */
@WebServlet("/login")
public class MyLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doGet(req, resp);

        //处理中文乱码问题
        resp.setContentType("text/html;charset=utf-8");

        //获取输出流
        PrintWriter out = resp.getWriter();
        //页面输出
        out.println("登录成功,欢迎你!<br /><br />");
        out.println("Welcome,Login Successfully!");
        //控制台输出
        System.out.println("登录成功");

        //刷新缓冲区,关闭输出流
        out.flush();
        out.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

2、启动类扫描web组件

java 复制代码
package com.hlx.springbootdemo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;

// @SpringBootApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//SpringBoot启动时扫描注册注解标注的Web组件
@ServletComponentScan
public class SpringbootDemo1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemo1Application.class, args);
    }

}

3、index.html页面

4、运行

相关推荐
lzp07911 小时前
元数据驱动开发 - 面向对象编程思想的补充(上)
spring boot·后端·ui
明月_清风9 小时前
加密解密系统完全指南:原理剖析与 Go 实践
后端
方也_arkling9 小时前
【Java-Day08】static / final / 枚举
java·开发语言
橙淮9 小时前
Spring Bean作用域与生命周期全解析
java·spring
Chengbei119 小时前
一站式源码安全检测工具、云安全 / APP / 小程序源码敏感信息递归多层目录扫描AK、JWT、手机号、身份证等敏感信息
java·开发语言·安全·web安全·网络安全·系统安全·安全架构
llz_1129 小时前
web-第一次课后作业
java·开发语言·idea
秋99 小时前
Java项目运行5天左右自动宕机:系统性定位与解决方案
java·开发语言·python
小江的记录本9 小时前
【JVM虚拟机】垃圾回收GC:垃圾收集器:CMS:核心原理、回收流程、优缺点、废弃原因(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·spring·面试·maven
DIY源码阁10 小时前
JavaSwing学生成绩管理系统 - MySQL版
java·数据库·mysql·eclipse
冬奇Lab10 小时前
每日一个开源项目(第105篇):Twenty - 跳出 Salesforce 的圈套,定义现代开源 CRM
前端·后端·开源