<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 中提供靜態內容 > 原文: [http://zetcode.com/springboot/static/](http://zetcode.com/springboot/static/) Spring Boot 靜態內容顯示了如何在 Spring Boot 應用中提供靜態內容。 Spring 是流行的 Java 應用框架。 Spring Boot 致力于以最小的努力創建獨立的,基于生產級別的基于 Spring 的應用。 Spring Boot 自動添加位于以下任何目錄中的靜態 Web 資源: * `/META-INF/resources/` * `/resources/` * `/static/` * `/public/` 目錄位于類路徑或`ServletContext`的根目錄中。 在我們的應用中,我們有一個 HTML 文件,其中包含一個簡單的鏈接。 該鏈接觸發來自 Web Boot 應用的響應。 它返回純文本消息。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───controller │ │ │ MyController.java │ │ └───model │ │ Message.java │ └───resources │ │ application.properties │ └───static │ │ index.html │ └───css │ main.css └───test └───java └───com └───zetcode └───controller MyControllerTest.java ``` 這是 Spring Boot 應用的項目結構。 `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>springbootstaticex</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-test</artifactId> <scope>test</scope> </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 應用的入門。 `spring-boot-starter-test`導入必要的測試模塊。 該應用打包到一個 JAR 文件中。 `com/zetcode/model/Message.java` ```java package com.zetcode.model; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Message { @Value("${app.message}") private String message; public String get() { return message; } } ``` `Message`設置 Spring Boot 應用。 ```java @Value("${app.message}") private String message; ``` 我們將`application.properties`中的值注入`message`變量中。 `resources/application.properties` ```java app.message=Hello there ``` `application.properties`文件包含 Spring Boot 應用的各種配置設置。 我們定義一個具有文本消息的自定義屬性。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import com.zetcode.model.Message; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { @Autowired private Message message; @GetMapping(path = "/message") @ResponseBody public String message() { return message.get(); } } ``` 這是 Spring Boot Web 應用的控制器類。 控制器以`@Controller`注解修飾。 控制器具有一個映射; 它被映射到`/message`路徑并返回純文本消息。 ```java @Autowired private Message message; ``` `Message`對象被注入到屬性中。 ```java @GetMapping(path = "/message") @ResponseBody public String message() { return message.get(); } ``` `message()`方法響應 GET 請求。 `@ResponseBody`注解將字符串值放入 Web 響應正文。 `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"> <link href="css/main.css" rel="stylesheet" type="text/css"> </head> <body> <h2>Home page</h2> <a href="/message">Get message</a> </body> </html> ``` 在`index.html`文件中,我們必須調用從 Web 應用的響應的鏈接。 該文件位于`src/main/resources/static`目錄中,該目錄是 Spring 尋找靜態內容的默認目錄。 ```java <link href="css/main.css" rel="stylesheet" type="text/css"> ``` 在鏈接標記中,我們指的是`main.css`靜態資源,該資源位于`src/main/resources/static/css`目錄中。 `resources/static/css/main.css` ```java h2 { color: blue } ``` 在`main.css`文件中,我們將`h2`標簽設置為藍色。 `com/zetcode/controller/MyControllerTest.java` ```java package com.zetcode.controller; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class) @SpringBootTest public class MyControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void getHome() throws Exception { this.mockMvc.perform(get("/")) .andDo(print()) .andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); } @Test public void getMessage() throws Exception { this.mockMvc.perform(get("/message")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("Hello there")); } } ``` 在`MyControllerTest`中,我們有兩個測試:一個用于主頁,另一個用于返回的消息文本。 `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`注解啟用自動配置和組件掃描。 在本教程中,我們在 Spring Boot 應用中提供了靜態上下文。 您可能也對相關教程感興趣: [Spring Boot `DataSourceBuilder`教程](/springboot/datasourcebuilder/), [Spring Boot iText 教程](/articles/springbootitext/), [Spring Boot RESTFul 應用](/articles/springbootrestsimple/), [Spring Web 應用簡介](/articles/springwebfirst/),[獨立的 Spring 應用](/articles/standalonespring/), [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>

                              哎呀哎呀视频在线观看