<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 中實現控制器 > 原文: [https://javatutorial.net/implementing-controllers-in-spring](https://javatutorial.net/implementing-controllers-in-spring) 控制器在 Spring 中的主要目的是攔截傳入的 http 請求,將數據發送到模型進行處理,最后從模型中獲取處理后的數據,然后將完全相同的數據傳遞給要呈現它的視圖。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) 上面所寫內容的頂級概述: ![Controllers in Spring example](https://img.kancloud.cn/e5/5b/e55bfdbf997a42f5e06bb5f246072e57_646x480.jpg) Spring 工作流程 現在,讓我們構建一個簡單的應用程序,作為在 Spring 中實現控制器的示例。 當您要“聲明”一個類作為控制器時,就像使用`@Controller`對其進行注釋一樣簡單。 在類級別使用時,控制器現在可以處理 REST API 請求。 ### `GetMapping`注解 當使用`@Controller`注解時,可以通過使用該方法的`RequestMapping`注解來提供請求映射。 還有`@RequestMapping`,`@PostMapping`,`@PutMapping`可以簡化常見 HTTP 方法類型的映射。 ```java package com.example.demo; 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; @RestController @SpringBootApplication public class DemoApplication { @GetMapping("/") String home() { return "Greetings from Java Tutorial Network"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 上面的代碼段,我們創建了一個`DemoApplication`,其注釋為`@Controller`和`@SpringBootApplication`。 請注意`@GetRequest("/")`的使用。 這就是說`home()`方法將顯示注釋中放置的內容,在本例中為`"/"`,即`http://localhost:8080/`。 在示例中,我使用的是`@RestConroller`,它基本上是控制器的專用版本,其中包括`@Controller`和`@ResponseBody`注解。 它只是簡化了控制器的實現。 當我們運行上面的代碼時,我們得到以下結果: ![Controllers implementation Spring](https://img.kancloud.cn/32/54/32540e6a32fa5c50e83811690633e3fb_961x178.jpg) 輸出量 如果我們想實現一個簡單的登錄功能,我們將執行以下操作: ```java package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/account/*") public class AccountController { @RequestMapping public String login() { return "login"; } } ``` 我們在這里所做的是,我們對類`AccountController`和方法`login()`都進行了注釋。 當訪問`http://localhost:8080/account/`時,我們將被帶到登錄頁面。 請注意網址末尾的`"/"`。 如果不存在,例如`http://localhost:8080/account`,將導致錯誤。 我在`account/`之后加上`*`的原因是,如果我們想向其添加更多內容(例如注冊),則該 URL 將能夠處理具有不同路徑的所有請求。 但就目前而言,我們將僅堅持登錄。 接下來,我們需要為登錄創建一個類,并將其注釋為`@Controller`: ```java package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/account/login") public class Login { @GetMapping public String login() { return "login"; } } ``` 現在,當我們訪問`http://localhost:8080/account/login`時,它將返回`login.jsp`文件中的內容。
                  <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>

                              哎呀哎呀视频在线观看