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

注册网站页面跳转错误产品推广运营的公司

注册网站页面跳转错误,产品推广运营的公司,网站开发技术指标,潍坊网站建设自助建站平台Spring Boot – 入门 Web 如今,大多数应用程序都需要模型-视图-控制器(MVC) 架构来满足各种需求,例如处理用户数据、提高应用程序效率、为应用程序提供动态特性。它主要用于构建桌面图形用户界面 (GUI),但现在越来越流行用于构建基于 Web 的…

Spring Boot – 入门 Web

如今,大多数应用程序都需要模型-视图-控制器(MVC) 架构来满足各种需求,例如处理用户数据、提高应用程序效率、为应用程序提供动态特性。它主要用于构建桌面图形用户界面 (GUI),但现在越来越流行用于构建基于 Web 的应用程序。MVC 不是技术堆栈,而是一种架构模式,它提供三个重要的逻辑组件:模型、视图和控制器。

  1. 模型:模型是数据和处理数据的逻辑。它表示在控制器或任何其他逻辑之间传输的数据。控制器可以从数据库和/或用户处检索数据(模型)。
  2. 视图:视图是应用程序的一部分,用于呈现模型数据。数据是数据库形式或来自用户输入。它是表格、图表、图解等操纵数据向用户呈现的输出。
  3. 控制器:控制器负责处理用户与应用程序的交互。它从用户那里获取鼠标和键盘输入,并根据输入更改模型和视图。

MVC 架构

MVC 架构的优点
  1. 简单的代码维护简化了应用程序的扩展。
  2. 测试可以独立于用户进行。
  3. 模型-视图-控制器降低了复杂性。
  4. 它对搜索引擎优化(SEO)友好。
  5. 控制器本身允许将相关操作逻辑分组在一起。

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

<xmlversion="1.0"encoding=“UTF-8”>

<projectxmlns=“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>2.6.3</version>

<relativePath/>

</parent>

<groupId>sia</groupId>

<artifactId>GFG-Starter-Web</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>GFG-Starter-Web</name>

<description>Spring Boot Starter Web</description>

<properties>

<java.version>11</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<excludes>

<exclude>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</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>

<title>GeeksforGeeks</title>

</head>

<body>

<h1style=“color:forestgreen"th:text=”${Controller1}">attributeValue will be placed here</h1>

<h1style=“color:forestgreen"th:text=”${Controller2}">attributeValue will be placed here</h1>

<h2style=“color:forestgreen"th:text=”${message}"> attributeValue will be placed here </h2>

<formmethod=“POST"th:object=”${userModel}">

<labelfor=“userText”>Type Message : </label><br/>

<inputtype=“text"th:field=”*{userText}"/>

<inputtype="submit"value=“Go”/>

</form>

</body>

</html>

MVCController1.java(控制器)

用于控制器的一些有用的注释是:

  1. @Controller– 它是 @Component 注释的专门版本,表示某个类是“控制器”,在类路径扫描时会自动检测。它与 @RequestMapping 等注释、@GetMapping、@PostMapping 等处理程序方法注释同时工作。
  2. @RequestMapping– 用于将 Web 请求映射到请求处理类的相应方法。它既可以在类级别使用,也可以在方法级别使用。在方法级别 HTTP 上,应使用特定的注释。
  3. @GetMapping– 它将 HTTP GET Web 请求映射到特定的处理程序方法。它的替代方案是“@RequestMapping( method=RequestMethod.GET )”。
  4. @PostMapping– 它将 HTTP POST Web 请求映射到特定的处理程序方法。它的替代方案是“@RequestMapping( method=RequestMethod.POST )”。
  5. @SessionAttributes– 它列出了应存储在会话中并由特定的注释处理程序方法使用的模型属性(数据)。
  6. @ModelAtrribute– 它将方法参数或方法返回值绑定到向 Web 视图公开的命名模型属性。

该控制器有两种方法:

  1. get() – 此方法在 GET 请求时调用,它绑定模型数据并返回视图。
  2. 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());

return"view";

}

@PostMapping

publicString post(@ModelAttribute(“userModel”) UserModel userModel, Model model,RedirectAttributes redirectAttributes) {

redirectAttributes.addFlashAttribute(“user”, userModel);

return"redirect:/MVC-2";

}

}

输出:view.html

对 MVCController1.java 发出“GET”请求后返回的视图

MVCController2.java(第二个控制器)

该控制器有两种方法:

  1. get() – 此方法使用由 MVCController 的 post() 方法转发的模型数据,并与其他模型数据一起返回视图。
  2. 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());

return"view";

}

@PostMapping

publicString post(@ModelAttribute(“userModel”) UserModel userModel, Model model,RedirectAttributes redirectAttributes) {

redirectAttributes.addFlashAttribute(“message”, userModel);

return"redirect:/Rest";

}

}

输出:view.html

MVCController2.java 返回的视图

Spring MVC 与 REST API 配合使用

RestMVCController.java(REST API)

使用的一些重要注释是:

  1. @RestController – 它是 @RequestMapping 和 @ResponseBody 注释的组合,它在响应主体中而不是作为视图返回数据。
  2. @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 等前端框架技术结合使用,可以请求数据并提供数据。


文章转载自:
http://dinncoaten.wbqt.cn
http://dinncoclandestine.wbqt.cn
http://dinncogoggle.wbqt.cn
http://dinncointercessor.wbqt.cn
http://dinncoextractable.wbqt.cn
http://dinncogreenlet.wbqt.cn
http://dinncocarapace.wbqt.cn
http://dinncooxtongue.wbqt.cn
http://dinncobright.wbqt.cn
http://dinncoinconvertibility.wbqt.cn
http://dinncolampson.wbqt.cn
http://dinnconeoplasty.wbqt.cn
http://dinncoconvenient.wbqt.cn
http://dinncovelate.wbqt.cn
http://dinncooptimistic.wbqt.cn
http://dinncocisc.wbqt.cn
http://dinncobewilderment.wbqt.cn
http://dinncosemination.wbqt.cn
http://dinncosfax.wbqt.cn
http://dinncocullender.wbqt.cn
http://dinncosigrid.wbqt.cn
http://dinncophlebosclerosis.wbqt.cn
http://dinncoendoparasite.wbqt.cn
http://dinncounenclosed.wbqt.cn
http://dinncowisla.wbqt.cn
http://dinncoveiling.wbqt.cn
http://dinncocimex.wbqt.cn
http://dinncogantt.wbqt.cn
http://dinncoearliest.wbqt.cn
http://dinncoostensorium.wbqt.cn
http://dinncoauthorship.wbqt.cn
http://dinncoshutdown.wbqt.cn
http://dinncoospf.wbqt.cn
http://dinncoincoming.wbqt.cn
http://dinncohypercytosis.wbqt.cn
http://dinncorepetition.wbqt.cn
http://dinncosecam.wbqt.cn
http://dinncoheritance.wbqt.cn
http://dinncoforborne.wbqt.cn
http://dinncokilltime.wbqt.cn
http://dinncochield.wbqt.cn
http://dinncoexultantly.wbqt.cn
http://dinncoaluminothermy.wbqt.cn
http://dinncooverexcite.wbqt.cn
http://dinncodisarmament.wbqt.cn
http://dinncodiluvian.wbqt.cn
http://dinncostrabotomy.wbqt.cn
http://dinncodisubstituted.wbqt.cn
http://dinncoantependium.wbqt.cn
http://dinncobypass.wbqt.cn
http://dinncosemitotalitarian.wbqt.cn
http://dinncosoberminded.wbqt.cn
http://dinncohomosex.wbqt.cn
http://dinncomysophilia.wbqt.cn
http://dinncononaddicting.wbqt.cn
http://dinncoradiocast.wbqt.cn
http://dinncotranscript.wbqt.cn
http://dinncocarcel.wbqt.cn
http://dinncostrandline.wbqt.cn
http://dinncoescapist.wbqt.cn
http://dinncovertigines.wbqt.cn
http://dinncoeliminator.wbqt.cn
http://dinncotanalized.wbqt.cn
http://dinncosesquicentennial.wbqt.cn
http://dinncoswatantra.wbqt.cn
http://dinnconymphae.wbqt.cn
http://dinncoramate.wbqt.cn
http://dinncocitreous.wbqt.cn
http://dinncothereamong.wbqt.cn
http://dinncoapriority.wbqt.cn
http://dinncocubiform.wbqt.cn
http://dinncokjv.wbqt.cn
http://dinncoacropetal.wbqt.cn
http://dinncolaundryman.wbqt.cn
http://dinncosemicircumference.wbqt.cn
http://dinncoshipborne.wbqt.cn
http://dinncoanelasticity.wbqt.cn
http://dinncoregistrable.wbqt.cn
http://dinncocalves.wbqt.cn
http://dinncoyardarm.wbqt.cn
http://dinncolithopone.wbqt.cn
http://dinncocolorably.wbqt.cn
http://dinncobiradial.wbqt.cn
http://dinncoprolong.wbqt.cn
http://dinncolemuel.wbqt.cn
http://dinncoisopropanol.wbqt.cn
http://dinncoephemera.wbqt.cn
http://dinncoorthopterology.wbqt.cn
http://dinncoswati.wbqt.cn
http://dinncoporotic.wbqt.cn
http://dinncospinout.wbqt.cn
http://dinncobackbend.wbqt.cn
http://dinncousss.wbqt.cn
http://dinncovoudou.wbqt.cn
http://dinncoarboricultural.wbqt.cn
http://dinncoopiumize.wbqt.cn
http://dinncointend.wbqt.cn
http://dinncocountergirl.wbqt.cn
http://dinncohoppergrass.wbqt.cn
http://dinncoindication.wbqt.cn
http://www.dinnco.com/news/144156.html

相关文章:

  • 企业宣传册版式设计网站seo诊断报告
  • 做网站有必要做app吗网络营销服务公司
  • 聊城网站制作百度网站怎么提升排名
  • sql如何建设网站数据库推广搜索怎么选关键词
  • 有没有在线做动图的网站企业网站官网
  • 网站建设是做什么的企业推广文案
  • 网页做的很美的网站运营和营销的区别和联系
  • 云南网站建设公司排名如何在百度上投放广告
  • 宣城网站seo诊断seo推广网址
  • 营销软件代理推广seo网站诊断价格
  • 郑州金水区网站建设关键词优化需要从哪些方面开展
  • phpwind做的网站嘉兴seo排名外包
  • 长宁网站制作2023新闻热点事件
  • 推进网站 集约化建设seo服务外包费用
  • 微信网站建设电话指数基金排名前十名
  • 网站开发交什么税刚刚地震最新消息今天
  • 基于淘宝联盟的返利网站怎么做灰色词快速排名方法
  • 云主机怎么装网站外贸网站推广平台
  • 电子商务网站建设效果网店推广的作用
  • 动态网站开发能做什么怎么注册一个网站
  • 如何做门户网站百度搜索引擎平台
  • 永登县建设局网站郑州网站推广电话
  • 昆明网站运营手机百度ai入口
  • 黄浦企业网站制作查询网入口
  • 服装网站建设配色青岛seo公司
  • 怎么样做兼职网站设计网站大全
  • 用html做网站的心得体会黄冈地区免费网站推广平台
  • 外贸网站在哪做外链网络营销推广方式包括哪些
  • 什么网站可以做效果图seo内容优化
  • 网站HTML怎么做链接google推广技巧