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、运行

相关推荐
丁卯4043 分钟前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
chengooooooo4 分钟前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦6 分钟前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个7 分钟前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
Tirzano37 分钟前
springsecurity自定义认证
spring boot·spring
南宫生3 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长3 小时前
Maven 基础环境搭建与配置(一)
java·maven
bing_1584 小时前
简单工厂模式 (Simple Factory Pattern) 在Spring Boot 中的应用
spring boot·后端·简单工厂模式
天上掉下来个程小白4 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
风与沙的较量丶4 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言