当前位置: 首页 > news >正文

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 页面

<!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

#app{background: lightcyan;height: 100vh;width: auto;display: flex;justify-content: center;align-items: center;
}button{width: 100px;height: 150px;
}

index.js

function my(){alert("测试JS!");
}

运行:

六、SpringBoot整合Web开发Servlet

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

1、创建MyLoginServlet类

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 {@Overrideprotected 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();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}
}

2、启动类扫描web组件

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

http://www.lryc.cn/news/487803.html

相关文章:

  • 国产三维CAD 2025新动向:推进MBD模式,联通企业设计-制造数据
  • ubuntu 之 安装mysql8
  • Flink Lookup Join(维表 Join)
  • Elasticsearch retrievers 通常与 Elasticsearch 8.16.0 一起正式发布!
  • 【并发模式】Go 常见并发模式实现Runner、Pool、Work
  • 【前端知识】Javascript前端框架Vue入门
  • Springboot3.3.5 启动流程之 Bean创建流程
  • golang反射函数注册
  • 【Spring】Bean
  • 深入解析TK技术下视频音频不同步的成因与解决方案
  • 为什么要使用Ansible实现Linux管理自动化?
  • Android:任意层级树形控件(有效果图和Demo示例)
  • C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
  • C++---类型转换
  • CSS基础学习练习题
  • TypeScript知识点总结和案例使用
  • 解决BUG: Since 17.0, the “attrs“ and “states“ attributes are no longer used.
  • 单片机GPIO中断+定时器 实现模拟串口接收
  • 《深入理解 Spring MVC 工作流程》
  • HTML简介
  • Linux系统Centos设置开机默认root用户
  • 【网络安全 | 甲方建设】双/多因素认证、TOTP原理及实现
  • Nuxt3 动态路由URL不更改的前提下参数更新,NuxtLink不刷新不跳转,生命周期无响应解决方案
  • 2024华为java面经
  • 2021 年 9 月青少年软编等考 C 语言三级真题解析
  • 深度解析FastDFS:构建高效分布式文件存储的实战指南(下)
  • Python学习29天
  • Soul App创始人张璐团队携多模态大模型参加GITEX GLOBAL,展现未来社交趋势
  • 简单工厂模式、方法工厂模式
  • 【面试】前端vue项目架构详细描述