<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring Boot `Model` > 原文: [http://zetcode.com/springboot/model/](http://zetcode.com/springboot/model/) Spring Boot `Model`教程展示了如何在 Spring Boot 應用中使用模型。 在 Spring,模型由`Model`,`ModelMap`和`ModelAndView`表示。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## MVC MVC(模型-視圖-控制器)是一種軟件架構模式,它將應用分為三個部分:模型,視圖和控制器。 該模型表示一個攜帶數據的 Java 對象。 該視圖使模型包含的數據可視化。 控制器管理流入模型對象的數據流,并在數據更改時更新視圖。 它使視圖和模型分離。 ## Spring MVC Spring MVC 是基于 Servlet API 構建的原始 Web 框架。 它基于 MVC 設計模式。 Spring Framework 5.0 引入了一個名為 Spring WebFlux 的并行反應式棧 Web 框架。 ## `Model`,`ModelMap`,`ModelAndView` `Model`,`ModelMap`和`ModelAndView`用于在 Spring MVC 應用中定義模型。 `Model`定義了模型屬性的持有者,并且主要用于向模型添加屬性。 `ModelMap`是`Model`的擴展,具有在映射和鏈式方法調用中存儲屬性的能力。 `ModelAndView`是模型和視圖的支架; 它允許在一個返回值中返回模型和視圖。 ## Spring Boot `Model`示例 以下簡單的 Web 應用在控制器方法中使用`Model`,`ModelMap`和`ModelAndView`。 該模型保存應用數據,該數據顯示在視圖中。 我們將 Freemaker 庫用于視圖層。 ```java pom.xml src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ └── controller │ │ └── MyController.java │ └── resources │ ├── application.properties │ ├── static │ │ └── index.html │ └── templates │ └── show.ftl └── test └── java ``` 這是 Spring 應用的項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springbootmodelex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven `pom.xml`文件。 `spring-boot-starter-parent`是父 POM,它為使用 Maven 構建的應用提供依賴關系和插件管理。 `spring-boot-starter-freemarker`是 Freemarker 模板引擎的依賴項。 此依賴項還將在項目中包含 Spring MVC。 `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `resources/application.properties` ```java spring.main.banner-mode=off logging.level.org.springframework=ERROR mymessage=Hello there ``` `application.properties`是 Spring Boot 中的主要配置文件。 我們通過選擇錯誤消息來關閉 Spring 橫幅,并減少 Spring 框架的日志記錄量。 `mymessage`屬性包含該消息。 `com/zetcode/MyController.java` ```java package com.zetcode.controller; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Locale; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class MyController { @Value("${mymessage}") private String message; @GetMapping("/getMessage") public String getMessage(Model model) { model.addAttribute("message", message); return "show"; } @GetMapping("/getMessage2") public ModelAndView getMessage() { var mav = new ModelAndView(); mav.addObject("message", message); mav.setViewName("show"); return mav; } @GetMapping("/getMessageAndTime") public String getMessageAndTime(ModelMap map) { var ldt = LocalDateTime.now(); var fmt = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM); fmt.withLocale(new Locale("sk", "SK")); fmt.withZone(ZoneId.of("CET")); var time = fmt.format(ldt); map.addAttribute("message", message).addAttribute("time", time); return "show"; } } ``` 這是`MyController`。 它具有三種響應客戶端請求的方法。 ```java @Controller public class MyController { ``` `MyController`帶有`@Controller`注解。 ```java @Value("${mymessage}") private String message; ``` 使用`@Value`注解,我們將`application.properties`文件中的`mymessage`屬性插入到`message`屬性中。 ```java @GetMapping("/getMessage") public String getMessage(Model model) { model.addAttribute("message", message); return "show"; } ``` `@GetMapping`將`/getMessage` URL 模式映射到`getMessage()`方法。 在`getMessage()`方法中,我們使用`Model`。 它使用`addAttribute()`方法接收`message`屬性。 return 關鍵字返回視圖的名稱,該名稱將解析為`show.ftl`,因為我們使用的是 Freemarker 模板系統。 ```java @GetMapping("/getMessage2") public ModelAndView getMessage() { var mav = new ModelAndView(); mav.addObject("message", message); mav.setViewName("show"); return mav; } ``` 在第二種情況下,我們使用`ModelAndView`。 我們使用`addObject()`和`setViewName()`添加模型數據和視圖名稱。 該方法返回`ModelAndView`對象。 ```java @GetMapping("/getMessageAndTime") public String getMessageAndTime(ModelMap map) { var ldt = LocalDateTime.now(); var fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); fmt.withLocale(new Locale("sk", "SK")); fmt.withZone(ZoneId.of("CET")); var time = fmt.format(ldt); map.addAttribute("message", message).addAttribute("time", time); return "show"; } ``` 在`getMessageAndTime()`方法中,我們使用`ModelMap`。 模型圖接收兩個屬性:消息和時間。 `resources/static/index.html` ```java <!DOCTYPE html> <html> <head> <title>Home page</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <ul> <li><a href="getMessage">Get message</a></li> <li><a href="getMessage2">Get message 2</a></li> <li><a href="getMessageAndTime">Get message and time</a></li> </ul> </body> </html> ``` 這是主頁。 它包含三個調用 Spring 控制器方法的鏈接。 它是靜態資源,位于預定義的`src/main/resources/static`目錄中。 `resources/templates/show.ftl` ```java <!DOCTYPE html> <html> <head> <title>Message</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p> Message: ${message} </p> <#if time??> <p>Date and time: ${time}</p> </#if> </body> </html> ``` `show.ftl`是一個 Freemarker 模板文件。 它位于預定義的`src/main/resources/templates`目錄中。 它使用`${}`語法輸出消息和時間(可選)。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`是設置 Spring Boot 應用的入口。 `@SpringBootApplication`注解啟用自動配置和組件掃描。 在掃描過程中,將查找`@Controller`注解,并從`MyController`類創建一個 Spring bean。 ```java $ mvn spring-boot:run ``` 啟動應用后,我們導航到`localhost:8080`。 在本教程中,我們已經在 Spring 應用中使用了模型。 您可能也對相關教程感興趣: [Spring Boot `@ModelAttribute`教程](/springboot/modelattribute/), [Spring Boot `@Controller`教程](/springboot/controller/), [Spring Boot Freemaker 教程](/springboot/freemarker/), [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Java 教程](/lang/java/)或列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看