当前位置: 首页 > 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://www.hkea.cn/news/778197/

相关文章:

  • 做搜狗网站优化首自己建立网站步骤
  • 企业资质查询官方网站最好的小说网站排名
  • 乐平网站设计北京互联网公司
  • 朝阳企业网站建设方案费用郑州网络营销学校
  • 建站行业发展百度广告代运营
  • 如何做积分商城网站鸡西seo顾问
  • p2p网站开发文档免费b站软件下载
  • 有没有做q版头像的网站今天百度数据
  • wordpress页面修改插件seo顾问阿亮
  • 政府门户网站建设标准国际婚恋网站排名
  • 上海青浦网站建设郑州靠谱seo电话
  • 网站建设怎么样seo专家招聘
  • 在网盘上怎么做自己的网站整站优化推广
  • php建设网站实训百度搜索引擎的总结
  • 怎么在360自己做网站重庆seo排名收费
  • 外贸网站建设浩森宇特教育培训报名
  • 网站开发价目表深圳市前十的互联网推广公司
  • php做视频直播网站关键词竞价广告
  • 重庆怎么站seo深圳网络推广团队
  • 自学软件网站开发网络推广怎样做
  • 最新版的wordpress怎么添加特征图优化关键词的作用
  • 深圳做网站google推广网络营销和传统营销的区别和联系
  • 专业做网站的顺德公司网络推广怎么收费
  • php商城网站建设多少钱天津百度seo排名优化
  • 注册网站免费注册insseo关键词优化推广哪家好
  • 深圳房地产网站开发常见的网络营销工具有哪些
  • .net 网站管理系统湖南企业竞价优化首选
  • 南山区住房与建设局官方网站网络赚钱推广
  • wordpress mycred汉化seo引擎搜索入口
  • 在线教育网站用什么做百度搜索的优势