Java框架spring Boot學習筆記(三):Controller的使用

Controller註解介紹html

  • @Controller:處理http請求
  • @RestController: Spirng4以後新加的註解,實際上是一個組合註解等同於@ResponseBody和@Controller的組合
  • @RequestMapping: 用於配置url映射,指望用戶經過url訪問方法
  • @PathVariable:獲取url中的數據
  • @RequestParam:使用和@PathVariable差很少,不過以?id=來傳遞值

 

@Controller的使用java

須要在之前的代碼結構基礎上修改三個文件web

 

爲pom.xml添加一個依賴spring

1 <dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-thymeleaf</artifactId>
4 </dependency>

右擊pom.xml,Reimport一下 app

 

新建一個index.html文件,添加一句話spring-boot

<h1>Hello Spring Boot!</h1>

 

修改HelloController.java,使用Controller註解,並返回index.html文件ui

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 @Controller  9 public class HelloController {
10 
11     //自動裝配註解
12     @Autowired
13     private PeopleProperties peopleProperties;
14 
15     @RequestMapping(value = "/hello" , method = RequestMethod.GET)
16     public String say() {
17         return "index";
18     }
19 }

 

運行,訪問本地8082端口url

 

 

@RestController的使用spa

@RestController至關於@ResponseBody和@Controller的組合註解code

修改HelloController.java

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController  9 public class HelloController {
10 
11     @Autowired
12     private PeopleProperties peopleProperties;
13 
14     @RequestMapping(value = {"/hello"} , method = RequestMethod.GET)
15     public String say() {
16         return peopleProperties.getName();
17     }
18 }

運行,訪問本地8082端口

 

@PathVariable的使用

 修改HelloController.java,獲取url中的id值顯示到頁面上

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 @ResponseBody
11 @Controller
12 public class HelloController {
13 
14     @Autowired
15     private PeopleProperties peopleProperties;
16 
17     @RequestMapping(value = {"/hello/{id}"} , method = RequestMethod.GET)
18     public String say(@PathVariable("id") Integer id) {
19         return "id: " +id;
20     }
21 }

運行,url中的id設爲2

 

@RequestParam的使用

 修改HelloController.java,用傳統的方式?id=value,來獲取url中的id值顯示到頁面上。

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.*;
 6 
 7 @ResponseBody
 8 @Controller
 9 public class HelloController {
10 
11     @Autowired
12     private PeopleProperties peopleProperties;
13 
14     @RequestMapping(value = {"/hello"} , method = RequestMethod.GET)
15     public String say(@RequestParam(value = "id" ,required = false,defaultValue = "0") Integer id) {
16         return "id: " +id;
17     }
18 }

運行

相關文章
相關標籤/搜索