spring后台controller层注解说明

@Controller  //  @Component @Service @Repository 作用一样  HelloController 交给spring容器负责初始化和管理

// 相当于 在spring的配置文件中 编写 <bean>

@RequestMapping("/hello")  // 映射请求  可以省略  企业中 模块的概念

public class HelloController {

    @RequestMapping("show")  // 不可以省略  / 可以省略不写,spring mvc加上

    public String show(Model model) {

        model.addAttribute("msg", "你好!");

        return "hello";

    }

//    @RequestMapping("show")  // 不可以省略  / 可以省略不写,spring mvc加上

//    public String show2() {

//        return "hello";

//    }

    @RequestMapping("/show2")

    public ModelAndView show2() {

        ModelAndView mav = new ModelAndView("hello");

        // 存放数据

        mav.addObject("msg", "我很好!");

        return mav;

    }

    @RequestMapping("/show3")

    @ResponseBody  // ResponseBody : 将方法的返回值以 json 格式返回   原则: 除了返回视图页面以及跳转页面之后,都需要添加 @ResponseBody

    public String show3() {

        return "hello";

    }

    // 占位符的映射

    // 删除书籍信息: http://localhost:8080/book/delete?bid=1

    // 新的请求方式: http://localhost:8080/book/delete/1  占位符请求

    // 新的请求方式: http://localhost:8080/hello/show4/1  占位符请求

//    @RequestMapping("/show4/{bid}")

//    public String show4(@PathVariable(value = "bid") int bid) {

//        System.out.println("bid:===" + bid);

//        return "hello";

//    }

    // 如果一个注解中 只有一个 值 那么 value可以省略

//    @RequestMapping("/show4/{bid}")

//    public String show4(@PathVariable("bid") int bid) {

//        System.out.println("bid:===" + bid);

//        return "hello";

//    }

    // 新的请求方式: http://localhost:8080/hello/show4/1  占位符请求  @Path 路径   Variable 变量  js 定义变量 var a = 1;

    @RequestMapping("/show4/{bid}")

    public String show4(@PathVariable int bid) {

        System.out.println("bid:===" + bid);

        return "hello";

    }

    @RequestMapping("/show5/{bid}/{name}")

    public String show5(@PathVariable int bid, @PathVariable String name) {

        System.out.println("bid:===" + bid);

        System.out.println("name:===" + name);

        return "hello";

    }

    // 限定请求方法的映射  后台的方法 值允许一种请求方式 或者 多个请求方式   8种

//    @RequestMapping("/show6")  // 没有限制   任何的请求方式都可以访问

    // 限制有两种方式:

//    @RequestMapping(value = "/show6", method = RequestMethod.POST)  // 只允许POST请求

//    @PostMapping("/show6")

    // 允许多种请求

    @RequestMapping(value = "/show6", method = {RequestMethod.GET, RequestMethod.POST}) // get post 支持

    public String show6() {

        return "hello";

    }

    // RequestParam

    // http://localhost:8080/hello/show7?name=zhangsan

    @RequestMapping("/show7")

    public String show7(String name) {

        System.out.println("name=====" + name);

        return "hello";

    }

    // 如果前台参数给了name 属性那么 就是用传递过来的值

    // defaultValue  默认值  没有给这个参数也可以 使用默认值

    @RequestMapping("/show8")

    public String show8(@RequestParam(defaultValue = "jack") String name) {

        System.out.println("name=====" + name);

        return "hello";

    }

    // 接收servlet的内置对象  HttpServletRequest HttpServletResponse HttpSession...

    // 两种方式:

    // 1: 直接写在方法的参数中 要求才controller

    // 2: 直接注入的形式  @Autowired

    @RequestMapping("/show9")

    public String show9(HttpServletRequest request) {

        String name = request.getParameter("name");

        System.out.println("name = ====" + name);

        return "hello";

    }

    @Autowired

    HttpServletRequest request;

    @Autowired

    HttpSession session;

    @RequestMapping("/show10")

    public String show10() {

        String age = request.getParameter("age");

        System.out.println("age======" + age);

        return "hello";

    }

    // @ResponseBody  :  将方法的返回值以json 的格式 返回  将一个java 对象 转换成 json

    // @RequestBody  :  将一个 json 字符串 转换成 Java对象

    // 设计到了 Json 转换 问题

    // 如何将一个对象 转换成 Json 字符串 呢 ???

    private ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping("/show11")

    public String show11() throws IOException {

        // 希望 将 Student 对象 序列化成  Json 字符串

        Student student = new Student();

        student.setAddress("上海");

        student.setStuName("小李子");

        student.setSid(1);

        // 1: alibaba fastjson  2: goggle Gson  3: jackson  springmvc内置的json转换包

        String jsonStr = objectMapper.writeValueAsString(student);

        System.out.println("jsonStr======" + jsonStr);

        // 将一个 json 字符串反序列化 成 Java对象

        String str = "{\"sid\":1,\"stuName\":\"小李子\",\"address\":\"上海\"}";

        Student stu = objectMapper.readValue(str, Student.class);

        System.out.println("student==========" + stu);

        return "hello";

    }

    // 转发 和 重定向

    // 如果需要进行页面或者地址的跳转

    // 目前: 前端页面跳转  location.href = "xxxxxxx";

    // 后端的路径的跳转怎么实现  ???

    @RequestMapping("/show12")

    public String hello() {

//        return "redirect:/hello/show11";  // 重定向  redirect:

        return "forward:/hello/show11";  // 转发  forward:

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值