<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring Boot `@ModelAttribute` > 原文: [http://zetcode.com/springboot/modelattribute/](http://zetcode.com/springboot/modelattribute/) Spring Boot `@ModelAttribute`教程顯示了如何在 Spring 應用中使用`@ModelAttribute`注解。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## `@ModelAttribute` `@ModelAttribute`將方法參數或方法返回值綁定到已公開的 Web 視圖的命名模型屬性。 用`@ModelAttribute`注解的方法在使用`@RequestMapping`的控制器方法之前被調用。 ## Spring Boot `@ModelAttribute`示例 以下應用演示了`@ModelAttribute`的用法。 它用于在應用中生成當天的消息。 該消息是從屬性文件中讀取的。 ```java pom.xml src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ ├── controller │ │ │ └── MyController.java │ │ └── service │ │ ├── IMessageService.java │ │ └── MessageService.java │ └── resources │ ├── application.properties │ ├── static │ │ └── index.html │ └── templates │ ├── pageOne.html │ └── pageTwo.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>springbootmodelattributeex</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 `pom.xml`文件。 `spring-boot-starter-parent`是父 POM,它為使用 Maven 構建的應用提供依賴關系和插件管理。 `spring-boot-starter-thymeleaf`是使用 Thymeleaf 視圖構建 MVC Web 應用的入門工具。 `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `resources/application.properties` ```java spring.main.banner-mode=off logging.level.org.springframework=ERROR messages.motd=Welcome ``` `application.properties`是 Spring Boot 中的主要配置文件。 我們通過選擇錯誤消息來關閉 Spring 橫幅,并減少 Spring 框架的日志記錄量。 `messages.motd`屬性包含該消息。 `com/zetcode/service/IMessageService.java` ```java package com.zetcode.service; public interface IMessageService { String getMessage(); } ``` `IMessageService`包含`getMessage()`合約方法。 `com/zetcode/service/MessageService.java` ```java package com.zetcode.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class MessageService implements IMessageService { @Value("${messages.motd}") private String motd="Hello"; @Override public String getMessage() { return motd; } } ``` `getMessage()`方法的實現使用`@Value`注解從屬性文件中檢索消息。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.zetcode.service.IMessageService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; @Controller public class MyController { @Autowired private IMessageService messageService; @GetMapping("/pageOne") public String getPageOne() { return "pageOne"; } @GetMapping("/pageTwo") public String getPageTwo() { return "pageTwo"; } @ModelAttribute("motd") public String message() { return messageService.getMessage(); } } ``` 由于`MyController`帶有`@Controller`注解,因此它成為 Spring MVC 控制器類。 使用`@GetMapping`注解,我們將兩個 URL 模式映射到 Thymeleaf 視圖。 這兩個模板都接收`motd`模型屬性。 ```java @ModelAttribute("motd") public String message() { return messageService.getMessage(); } ``` 在`@RequestMapping`方法及其特化(例如`@GetMapping`)之前,執行帶有`@ModelAttribute`注解的方法。 從`messageService`生成的消息存儲在`motd`模型屬性中,并且可用于兩個 Thymeleaf 視圖。 `resources/templates/pageOne.html` ```java <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Page one</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h2>Page one</h2> <p th:text="'Message of the day: ' + ${motd}"></p> </body> </html> ``` 這是`pageOne.html`視圖。 使用`${}`語法訪問`motd`屬性。 `resources/templates/pageTwo.html` ```java <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Page two</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h2>Page two</h2> <p th:text="'Message of the day:' + ${motd}"></p> </body> </html> ``` 這是`pageTwo.html`視圖。 `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> <a href="pageOne">Go to page one</a><br> <a href="pageTwo">Go to page two</a> </body> </html> ``` 這是主頁。 它包含兩個鏈接。 `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`注解啟用自動配置和組件掃描。 它是`@Configuration`,`@EnableAutoConfiguration`和`@ComponentScan`注解的便捷注解。 ```java $ mvn spring-boot:run ``` 應用運行后,我們可以導航到`localhost:8080`。 在本教程中,我們展示了如何在 Spring 應用中使用`@ModelAttribute`注解。 您可能也對相關教程感興趣: [Spring Boot 模型教程](/springboot/model/), [Spring Boot `@PathVariable`教程](/springboot/pathvariable/), [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Spring Boot `@ResponseBody`教程](/springboot/responsebody/), [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>

                              哎呀哎呀视频在线观看