muse网站设计解决方案视频教程,学编程官网,顺的网站建设精英,reactjs 做网站Spring Boot – 入门 Web
如今#xff0c;大多数应用程序都需要模型-视图-控制器(MVC) 架构来满足各种需求#xff0c;例如处理用户数据、提高应用程序效率、为应用程序提供动态特性。它主要用于构建桌面图形用户界面 (GUI)#xff0c;但现在越来越流行用于构建基于 Web 的…Spring Boot – 入门 Web
如今大多数应用程序都需要模型-视图-控制器(MVC) 架构来满足各种需求例如处理用户数据、提高应用程序效率、为应用程序提供动态特性。它主要用于构建桌面图形用户界面 (GUI)但现在越来越流行用于构建基于 Web 的应用程序。MVC 不是技术堆栈而是一种架构模式它提供三个重要的逻辑组件模型、视图和控制器。
模型模型是数据和处理数据的逻辑。它表示在控制器或任何其他逻辑之间传输的数据。控制器可以从数据库和/或用户处检索数据模型。视图视图是应用程序的一部分用于呈现模型数据。数据是数据库形式或来自用户输入。它是表格、图表、图解等操纵数据向用户呈现的输出。控制器控制器负责处理用户与应用程序的交互。它从用户那里获取鼠标和键盘输入并根据输入更改模型和视图。 MVC 架构
MVC 架构的优点
简单的代码维护简化了应用程序的扩展。测试可以独立于用户进行。模型-视图-控制器降低了复杂性。它对搜索引擎优化SEO友好。控制器本身允许将相关操作逻辑分组在一起。
Spring 的核心功能是 Spring MVC – Spring 的 Web 框架。它打包在“Spring Web”启动器中。Spring MVC 还可用于创建产生非 HTML 输出例如 JSON、XML 等的REST API 。
在 Spring 应用程序中嵌入 Started Web
Spring Tool Suite (STS) - Go To File - New - Spring Starter Project - Next - Spring Web - Next - Finish
您还可以添加 Starter Web 依赖项。
Maven - pom.xml org.springframework.boot spring-boot-starter-web
Gradle - build.gradle
dependencies { compile(“org.springframework.boot:spring-boot-starter-web”) }
Spring MVC 项目结构——Maven
pom.xml项目配置
XML
xmlversion1.0encoding“UTF-8”
projectxmlns“http://maven.apache.org/POM/4.0.0”
xmlns:xsi“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd
modelVersion4.0.0/modelVersion
parent
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-parent/artifactId
version2.6.3/version
relativePath/
/parent
groupIdsia/groupId
artifactIdGFG-Starter-Web/artifactId
version0.0.1-SNAPSHOT/version
nameGFG-Starter-Web/name
descriptionSpring Boot Starter Web/description
properties
java.version11/java.version
/properties
dependencies
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-thymeleaf/artifactId
/dependency
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-web/artifactId
/dependency
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-devtools/artifactId
scoperuntime/scope
optionaltrue/optional
/dependency
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-test/artifactId
scopetest/scope
/dependency
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter/artifactId
/dependency
dependency
groupIdorg.projectlombok/groupId
artifactIdlombok/artifactId
optionaltrue/optional
/dependency
/dependencies
build
plugins
plugin
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-maven-plugin/artifactId
configuration
excludes
exclude
groupIdorg.projectlombok/groupId
artifactIdlombok/artifactId
/exclude
/excludes
/configuration
/plugin
/plugins
/build
/project
GfgStarterWebApplication.java应用程序的引导
Java
packagegfg;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
SpringBootApplication
public********classGfgStarterWebApplication {
publicstaticvoidmain(String[] args) {
SpringApplication.run(GfgStarterWebApplication.class, args);
}
}
UserModel.java模型
Java bean 需要 getter 和 setter 方法来更新和访问变量。‘Lombok’ Java 库使用’Data’注释自动执行 Getter 和 Setter 方法的创建过程。要将 Lombok 包含在项目中请添加以下依赖项
Maven - pom.xml org.projectlombok lombok true
Java
packagegfg;
importlombok.Data;
Data
public********classUserModel {
publicString userText;
}
view.html视图 -Thymeleaf 模板
读取用户输入绑定到UserModel从而创建Model数据提交后将Model数据传递给Controller。
HTML
html
head
titleGeeksforGeeks/title
/head
body
h1style“color:forestgreenth:text”${Controller1}attributeValue will be placed here/h1
h1style“color:forestgreenth:text”${Controller2}attributeValue will be placed here/h1
h2style“color:forestgreenth:text”${message} attributeValue will be placed here /h2
formmethod“POSTth:object”${userModel}
labelfor“userText”Type Message : /labelbr/
inputtype“textth:field”*{userText}/
inputtypesubmitvalue“Go”/
/form
/body
/html
MVCController1.java控制器
用于控制器的一些有用的注释是
Controller– 它是 Component 注释的专门版本表示某个类是“控制器”在类路径扫描时会自动检测。它与 RequestMapping 等注释、GetMapping、PostMapping 等处理程序方法注释同时工作。RequestMapping– 用于将 Web 请求映射到请求处理类的相应方法。它既可以在类级别使用也可以在方法级别使用。在方法级别 HTTP 上应使用特定的注释。GetMapping– 它将 HTTP GET Web 请求映射到特定的处理程序方法。它的替代方案是“RequestMapping( methodRequestMethod.GET )”。PostMapping– 它将 HTTP POST Web 请求映射到特定的处理程序方法。它的替代方案是“RequestMapping( methodRequestMethod.POST )”。SessionAttributes– 它列出了应存储在会话中并由特定的注释处理程序方法使用的模型属性数据。ModelAtrribute– 它将方法参数或方法返回值绑定到向 Web 视图公开的命名模型属性。
该控制器有两种方法
get() – 此方法在 GET 请求时调用它绑定模型数据并返回视图。post() – 此方法从用户的 POST 请求中获取模型数据该数据将被其他控制器使用并重定向到 MVCController2.java。
Java
packagegfg;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.PostMapping;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.servlet.mvc.support.RedirectAttributes;
importorg.springframework.web.bind.annotation.SessionAttributes;
Controller
SessionAttributes(“userModel”)
RequestMapping(“MVC-1”)
public********classMVCController1 {
GetMapping
publicString get(Model model) {
model.addAttribute(“Controller1”,“You are in Controller-1”);
model.addAttribute(“userModel”, newUserModel());
returnview;
}
PostMapping
publicString post(ModelAttribute(“userModel”) UserModel userModel, Model model,RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute(“user”, userModel);
returnredirect:/MVC-2;
}
}
输出view.html 对 MVCController1.java 发出“GET”请求后返回的视图
MVCController2.java(第二个控制器)
该控制器有两种方法
get() – 此方法使用由 MVCController 的 post() 方法转发的模型数据并与其他模型数据一起返回视图。post() – 此方法获取模型用户数据转发并重定向到 RestMVCController。
Java
packagegfg;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.PostMapping;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.servlet.mvc.support.RedirectAttributes;
importorg.springframework.web.bind.annotation.SessionAttributes;
Controller
SessionAttributes(“userModel”)
RequestMapping(“/MVC-2”)
public********classMVCController2 {
GetMapping
publicString get(ModelAttribute(“user”) UserModel message, Model model) {
model.addAttribute(“Controller2”,“You are in Controller-2”);
model.addAttribute(“message”, message.getUserText());
model.addAttribute(“userModel”, newUserModel());
returnview;
}
PostMapping
publicString post(ModelAttribute(“userModel”) UserModel userModel, Model model,RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute(“message”, userModel);
returnredirect:/Rest;
}
}
输出view.html MVCController2.java 返回的视图
Spring MVC 与 REST API 配合使用
RestMVCController.java(REST API)
使用的一些重要注释是
RestController – 它是 RequestMapping 和 ResponseBody 注释的组合它在响应主体中而不是作为视图返回数据。CrossOrigin – 用于允许处理程序类和/或处理程序方法上的跨域请求使用数据。
Java
packagegfg;
importorg.springframework.web.bind.annotation.CrossOrigin;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importorg.springframework.web.bind.support.SessionStatus;
importorg.springframework.web.bind.annotation.SessionAttributes;
RestController
SessionAttributes(“userModel”)
RequestMapping(path“/Rest”, produces“application/json”)
CrossOrigin(origins“*”)
public********classRestMVCController {
GetMapping
publicUserModel get(ModelAttribute(“message”) UserModel user, SessionStatus sessionStatus) {
// cleans up the stored
// session attributes (data)
sessionStatus.setComplete();
returnuser;
}
}
输出 在响应中返回模型数据JSON 文字
注意Spring 框架的后端 REST API 可以与 Angular、React 等前端框架技术结合使用可以请求数据并提供数据。