@ResponseBody 的作用
- 該方法的返回的結果 直接寫入
HTTP 響應正文(ResponseBody)
中。
Controller 層代碼:
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/login") // 頁面跳轉
@ResponseBody // 原樣輸出,不進行頁面跳轉
public String login() {
return "login";
}
}
- 瀏覽器輸入:
http://localhost:8080/demo/login
時,頁面直接顯示 “login”