三 Controller
1. 常用註解
@Controller 接收http請求
@RestController 是@Controller和@ResponseBody的結合
@RequestMapping url地址映射
@PathVariable 獲取url參數
@RequestParam 獲取請求參數的值
@GetMapping 組合註解 簡化get方法
2. 模板配置
在resources目錄下新建templates,在下面新建html模板(模板中隨便寫點東西,便於觀察)
pom.xml中添加配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
HelloController改成如下配置(主要修改類上的註解,和方法上的返回值)
package com.boot.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
@Value("${stu.name}")
private String cupSize;//配置文件注入
@Value("${stu.context}")
private String context;
@RequestMapping(value="/hello" ,method = RequestMethod.GET)
public String say(){
return "index";
//return "spring boot!";
}
}
在瀏覽器中輸入localhost:8080/hello 可以看到模板