HelloController.java

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模板引擎


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值