Day2 :狂神说--SpringMVC笔记2

**

  • 1.Spring:大杂烩

     		我们可以将SpringMVC中所有要用到的bean,注册到Spring中**
    
  • @Component 组件

  • @Service Service

  • @COntroller controller

  • @Repository dao

使用Model传送数据比使用ModelAndView更方便一点

Model:
//代表这个类会被Spring接管,被这个注解的类中的所有方法,如果返回值是String,并且有具体页面可以跳转,那么就会被视图解析器解析
@Controller
public class ControllerTest2 {
    @RequestMapping("/t2")
    public String test1(Model model){
        //model就是传数据专用的
        model.addAttribute("msg","ControllerTest2");

        return "hello";  //会返回给WEB-INF/jsp/hello.jsp
    }
}

ModelAndView:
public class ControllerTets1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //返回一个模型视图对象
        ModelAndView mv = new ModelAndView();
        //添加数据
        mv.addObject("msg","Hello Controller");
        //设置视图名字(跳转到哪里)
        mv.setViewName("hello");
        return mv;
    }
}
  • 2.RestFul风格讲解

REST(英文:Representational State Transfer,简称REST,意思:表述性状态转换,描述了一个架构样式的网络系统,比如web应用)。

它是一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件,它主要用于客户端和服务端交互类的软件。基于这个风格设计的软件可以更简介,更有层次,更易于实现缓存等机制

   // 原来的方式: http://localhost:8080/springmvc_05_controller_war_exploded/add?a=1&b=1

    @RequestMapping("/add1")
    public String test1(int a , int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果为"+res);
        return "hello";
    }

    //RestFul方式: http://localhost:8080/springmvc_05_controller_war_exploded/add/a/b
    @RequestMapping("/add2/{a}/{b}")
    public String test2(@PathVariable int a , @PathVariable int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果为"+res);
        return "hello";
    }
    //http:..../add2/1/3

在这里插入图片描述

@GetMapping("add1/{a}/{b}")

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值