<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 第一個 Web 應用 > 原文: [http://zetcode.com/articles/springbootwebfirst/](http://zetcode.com/articles/springbootwebfirst/) Spring Boot 的第一個 Web 應用教程展示了如何創建一個簡單的 Spring Boot Web 應用。 當前的趨勢是從可執行的 JAR 啟動 Spring Boot 應用。 (有關傳統 WAR 部署的示例,請參見[`SpringBootServletInitializer`教程](/springboot/springbootservletinitializer/)。) Spring 是流行的 Java 應用框架。 Spring Boot 致力于以最小的努力創建獨立的,基于生產級別的基于 Spring 的應用。 ## Spring Boot Web 應用示例 該應用顯示一條消息和今天的日期。 該消息是從應用的屬性中檢索的。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───controller │ │ MyController.java │ └───resources │ │ application.properties │ └───templates │ index.html └───test └───java ``` 這是項目結構。 `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>springbootfirstweb</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-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven 構建文件。 `spring-boot-starter-web`是使用 Spring MVC 構建 Web(包括 RESTful)應用的入門程序。 `spring-boot-starter-thymeleaf`包含 Thymeleaf 模板引擎。 當 Spring Boot 檢測到該啟動程序時,它將自動為我們配置 Thymeleaf。 該應用打包到一個 JAR 文件中,該文件包含一個嵌入式 Tomcat Web 服務器。 `resources/application.properties` ```java application.message: Hello there ``` `application.properties`文件包含 Spring Boot 應用的各種配置設置。 我們有一個自定義消息選項。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.time.LocalDate; import java.util.Map; @Controller public class MyController { @Value("${application.message}") private String message = "Hi there"; @GetMapping("/") public String index(Map<String, Object> model) { model.put("now", LocalDate.now()); model.put("message", this.message); return "index"; } } ``` 這是 Spring Boot Web 應用的控制器類。 控制器被飾以`@Controller`注解。 控制器具有一個映射。 映射解析為`index.jsp`,它位于`WEB-INF/jsp`目錄中。 ```java @Value("${application.message}") private String message = "Hi there"; ``` 我們將`application.properties`中的值注入`message`變量中。 ```java @GetMapping("/") public String index(Map<String, Object> model) { model.put("now", LocalDate.now()); model.put("message", this.message); return "index"; } ``` `@GetMapping`注解將帶有/路徑的 GET 請求映射到索引方法處理器。 創建一個模型并填充數據。 該模型是`Map`接口,可以完全抽象視圖技術。 `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 應用。 `resources/templates/index.html` ```java <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p th:text="'Date: ' + ${now}"></p> <p th:text="'Message: ' + ${message}"></p> </body> </html> ``` `index.html`顯示兩個值:當前日期和收到的消息。 這兩個值都通過控制器傳遞到模板。 ```java $ mvn spring-boot:run ``` 我們運行該應用。 現在我們可以導航到`localhost:8080`以查看應用消息。 在本教程中,我們創建了第一個 Spring Boot Web 應用。 您可能也對相關教程感興趣: [Spring Web 應用簡介](/articles/springwebfirst/),[獨立的 Spring 應用](/articles/standalonespring/), [FreeMarker 教程](/java/freemarker/), [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>

                              哎呀哎呀视频在线观看