<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 MVC 控制器 > 原文: [https://javatutorial.net/implementing-spring-mvc-controllers](https://javatutorial.net/implementing-spring-mvc-controllers) 本教程描述了實現 Spring MVC 控制器的不同方法,并提供了示例。 在我以前的教程,[使用 STS 創建簡單的 Spring Web App](https://javatutorial.net/spring-web-app-sts) 中,我向您展示了如何構建引入控制器的 Spring Boot App。 本教程是對上一個教程的擴展。 在開始實現之前,讓我們快速概述一下控制器如何參與 MVC 工作流程。 ![Spring MVC architecture workflow](https://img.kancloud.cn/35/64/3564a24931b1ad1eff6d525675180990_700x500.jpg) Spring MVC 架構工作流程 1. 來自客戶端的傳入請求由調度器 Servlet 解釋 2. 調度器 Servlet 通過解析請求屬性并使對象對處理程序可用來進行初始處理。 3. 確定并調用適當的處理程序以進一步處理請求。 確定適當控制器上的適當方法 4. 控制器處理請求并返回`ModelAndView`的實例 5. 調度器 Servlet 進一步處理`ModelAndView`的實例,以將響應發送給客戶端 ## 在 Spring Boot 應用程序中啟用 JSP 如果要啟用 JSP,則必須執行以下操作: 在`pom.xml`文件中添加以下依賴項: ```java <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> ``` 在`src/main/resources/application.properties`中添加這兩行 ```java spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp ``` 創建文件夾`src/main/resources/META-INF/resources/WEB-INF/jsp/`并將 JSP 文件放入其中 ## 實現 Spring 控制器返回 JSP 頁面 以下示例演示如何在 Spring `Controller`方法中返回 JSP 頁面。 請注意`@Controller`注釋和`@RequestMapping`注釋的用法。 如果我們想返回一個 JSP 頁面,我們將不使用`@ResponseBody`注釋(如第二個示例所示)。 ```java package net.javatutorial.tutorials; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @SpringBootApplication public class ControllerExampleJSP { @RequestMapping("/hellojsp") String helloJSP() { return("index"); } public static void main(String[] args) { SpringApplication.run(ControllerExampleJSP.class, args); } } ``` `@RequestMapping`注釋將網址`http://localhost:8080/hellojsp`插入到控制器的方法`helloJSP()`中。 此方法返回`index.jsp`的解析內容 ![Rendered JSP page using Spring controller](https://img.kancloud.cn/2e/0d/2e0d59443b313c344e55e8d61832660e_468x175.jpg) 使用 Spring 控制器渲染 JSP 頁面 ## 用`ResponseBody`實現控制器 與前面的示例不同,此示例將返回由方法而不是 JSP 頁面生成的`String`。 我們唯一需要更改的就是將`@ResponseBody`注解添加到我們的控制器方法中 ```java package net.javatutorial.tutorials; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @SpringBootApplication public class ControllerResponseBodyExample { @RequestMapping("/helloresponsebody") @ResponseBody String helloResponseBody() { return("Hello World. This is produced by a method annotated with ResponseBody"); } public static void main(String[] args) { SpringApplication.run(ControllerResponseBodyExample.class, args); } } ``` 在瀏覽器中調用`http://localhost:8080/helloresponsebody`將產生以下輸出: ![Output using Spring Controller and ResponseBody](https://img.kancloud.cn/68/22/68220cc487b837cb79ae4f0b32d48596_500x108.jpg) 使用 Spring `Controller`和`ResponseBody`進行輸出 ## 實現 Spring `RestController` `@RestController`注釋用作方便注釋,以表示諸如`@Controller`和`@ResponseBody`之類的注釋。 在類級別使用時,控制器可以處理 REST API 請求。 ```java package net.javatutorial.tutorials; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class RestControllerExample { @RequestMapping("/hellorest") String helloRest() { return("Hello World. This is produced by the rest conntroller method"); } public static void main(String[] args) { SpringApplication.run(RestControllerExample.class, args); } } ``` 在瀏覽器中調用`http://localhost:8080/hellorest`將產生以下輸出: ![Output using Spring RestController](https://img.kancloud.cn/c5/7b/c57beb39bc84ed1ed36963d1e1700dbf_443x142.jpg) 使用 Spring `RestController`輸出 ## 在方法和類級別使用`@RequestMapping`注釋 Spring 4.3 引入了諸如`@GetMapping`,`@PostMapping`和`@PutMapping`等注解,以指定常見 HTTP 方法類型(如 GET,POST 和 PUT)的映射。 這些注釋增強了代碼的可讀性。 以下示例演示了如何在方法和類級別上使用映射注釋。 ```java package net.javatutorial.tutorials; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user/*") @SpringBootApplication public class MethodAndClassLevelAnnotations { @RequestMapping String login() { return("Login method called"); } @GetMapping("/logout") String logout() { return("Logout method called"); } public static void main(String[] args) { SpringApplication.run(MethodAndClassLevelAnnotations.class, args); } } ``` 向以下網址`http://localhost:8080/user/`發出請求,將調用`login()`方法。 注意,注釋`login()`方法的`@RequestMapping`沒有參數。 在類級別使用的`@RequestMapping("/user/*")`注釋用作兜底方法,以使用`/*`表示的不同路徑來處理所有請求。 請求`http://localhost:8080/user/logout`將調用`logout()`方法。 `@GetMapping`注釋是一個組合的注釋,用作`@RequestMapping(method = RequestMethod.GET)` 您可以在[我們的 GitHub 存儲庫](https://github.com/JavaTutorialNetwork/Tutorials/tree/master/SpringImplementingControllers)中找到本教程中的代碼示例。
                  <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>

                              哎呀哎呀视频在线观看