package com.xs.controller;
import com.xs.properties.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//写法一:@RestController等同于@Controller+@ResponseBody的组合
@RestController
@RequestMapping("/hello") //给整个类制定一个URL,这时候要访问到/say方法就用到拼接,即http://127.0.0.1:8080/hello/say
//写法二:
//@Controller
//@ResponseBody
public class HelloController {
//新的取值方法
@Autowired
private GirlProperties girlProperties;
//原始取值方法
/* @Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;*/
//需求:用户hello或者hi任何一个访问都能够访问到这个方法--及用到了@RequestMapping注解,用到了URL映射,及集合的形式访问
//@RequestMapping(value = {"/hello","/hi"}, method = RequestMethod.GET)
//ID取值的两种写法
//写法一:这里由于是手动输入地址的方式访问,所有只能用GET方式,用表单提交方式都可以访问,不推荐不写method =,同时这里的id写在前后不影响
// 如下:
// @RequestMapping(value = "/say/{id}", method = RequestMethod.GET)
// @RequestMapping(value = "/{id}/say", method = RequestMethod.GET)
//public String say(@PathVariable("id") Integer id) {
// return "id:" + myId;
//}
//写法二:
@RequestMapping(value = "/say", method = RequestMethod.GET)
//传统的写法:即?=这种形式,注意的是(@RequestParam("id")这里的id名字必须与传过来的id名字一致,不然传不过来,后面的名字随意
//例子:http://127.0.0.1:8080/hello/say?id=111
//public String say(@RequestParam("id") Integer myId) {
//需求:没有参数的时候不为null,设置默认值
//写法三(简化版---推荐):
@GetMapping(value = "/say")
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId) {
return "id:" + myId;
// return girlProperties.getCupSize();
// return "index"; --这里调用了thymeleaf模板引擎
}
}
import com.xs.properties.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//写法一:@RestController等同于@Controller+@ResponseBody的组合
@RestController
@RequestMapping("/hello") //给整个类制定一个URL,这时候要访问到/say方法就用到拼接,即http://127.0.0.1:8080/hello/say
//写法二:
//@Controller
//@ResponseBody
public class HelloController {
//新的取值方法
@Autowired
private GirlProperties girlProperties;
//原始取值方法
/* @Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;*/
//需求:用户hello或者hi任何一个访问都能够访问到这个方法--及用到了@RequestMapping注解,用到了URL映射,及集合的形式访问
//@RequestMapping(value = {"/hello","/hi"}, method = RequestMethod.GET)
//ID取值的两种写法
//写法一:这里由于是手动输入地址的方式访问,所有只能用GET方式,用表单提交方式都可以访问,不推荐不写method =,同时这里的id写在前后不影响
// 如下:
// @RequestMapping(value = "/say/{id}", method = RequestMethod.GET)
// @RequestMapping(value = "/{id}/say", method = RequestMethod.GET)
//public String say(@PathVariable("id") Integer id) {
// return "id:" + myId;
//}
//写法二:
@RequestMapping(value = "/say", method = RequestMethod.GET)
//传统的写法:即?=这种形式,注意的是(@RequestParam("id")这里的id名字必须与传过来的id名字一致,不然传不过来,后面的名字随意
//例子:http://127.0.0.1:8080/hello/say?id=111
//public String say(@RequestParam("id") Integer myId) {
//需求:没有参数的时候不为null,设置默认值
//写法三(简化版---推荐):
@GetMapping(value = "/say")
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId) {
return "id:" + myId;
// return girlProperties.getCupSize();
// return "index"; --这里调用了thymeleaf模板引擎
}
}