Skip to main content

Controller的使用

[TOC]

注解

@Controller

==必须配合模板使用==

1. Maven新加

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. 创建模板文件

springboot2\src\main\resources\templates

新建一个 index.html 模板文件 内容随意

3. java这么写

package cn.asdasd.demo;

import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class HelloController {

@Autowired
private GirlPropertie girlPropertie;

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say() {
return "index";
}

}

@RestController 写接口用

@RestController 等于 @Controller + @ResponseBody 的组合

@RequestMapping("/hello") 设置整个页面的分组

@RequestMapping(value = {"/say","/hi"},method = RequestMethod.GET)

访问say 或 hi 都可以运行

method = RequestMethod.GET 请求方式,不加这个参数都可以访问

package cn.asdasd.demo;

import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@ResponseBody
@RequestMapping("/hello")
public class HelloController {

@Autowired
private GirlPropertie girlPropertie;

@RequestMapping(value = {"/say","/hi"},method = RequestMethod.GET)
public String say() {
return girlPropertie.getCupSize();
}
}

获取URL中的值

单一入口的参数

package cn.asdasd.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("/hello")
public class HelloController {

@Autowired
private GirlPropertie girlPropertie;

// http://localhost:8082/girl/hello/say/32
@RequestMapping(value = "say/{id}",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id) {
return "id:" + id;
}

// http://localhost:8082/girl/hello/hi?id=3123
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String hi(@RequestParam("id") Integer myid) {
return "id:" + myid;
}


// http://localhost:8082/girl/hello/hi3
@RequestMapping(value = "/hi3",method = RequestMethod.GET)
public String hi3(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myid) {
return "id:" + myid;
}
}

这么访问 http://localhost:8082/girl/hello/say/32

? 的参数

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
public String hi(@RequestParam("id") Integer myid) {
return "id:" + myid;
}

这样写就这么访问 http://localhost:8082/girl/hello/hi?id=3123

设置默认值

    // http://localhost:8082/girl/hello/hi3
@RequestMapping(value = "/hi3",method = RequestMethod.GET)
public String hi3(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myid) {
return "id:" + myid;
}

@GetMapping

可以简化 @RequestMapping(value = "/hi3",method = RequestMethod.GET) 的写法

同类型的还有 @PostMapping @PutMapping @DeleteMapping

    // http://localhost:8082/girl/hello/hi4
@GetMapping(value = "/hi4")
public String hi4(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myid) {
return "hi4id:" + myid;
}