2----------RestFul和控制器

控制器Controller

  • 控制器复杂提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现。

  • 控制器负责解析用户的请求并将其转换为一个模型。

  • 在Spring MVC中一个控制器类可以包含多个方法

  • 在Spring MVC中,对于Controller的配置方式有很多种

实现Controller接口

使用注解

RestFul风格

概念

RestFul就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更加简洁,更有层次,更易于实现缓存等机制。

功能

资源:互联网所有的事物都可以呗抽象为资源

资源定位:使用POST、DELETE、PUT、GET,使用不同的方法对资源进行操作。
分别对应添加、删除、修改、查询。

简单来说:就是用地址和请求方式,区分提交的服务。

 根据参数类型的不同

package com.sicheng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

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

    @RequestMapping("/hello1/{p1}/{p2}")
    public String hello1 (@PathVariable int p1, @PathVariable int p2, Model model){
        int result = p1 + p2;
        model.addAttribute("msg", result);
        return "test1";
    }

    @RequestMapping("/hello2/{p1}/{p2}")
    public String hello2 (@PathVariable int p1, @PathVariable String p2, Model model){
        String result = p1 + p2;
        model.addAttribute("msg", result);
        return "test2";
    }
}

 

 

 根据参数个数的不同

xxxx应该可以的吧,自己实现

根据提交方式的不同

package com.sicheng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

    @RequestMapping(value = "/hello1/{p1}/{p2}", method = RequestMethod.POST)
    public String hello1 (@PathVariable int p1, @PathVariable int p2, Model model){
        int result = p1 + p2;
        model.addAttribute("msg", result);
        return "test1";
    }

    @RequestMapping(value = "/hello1/{p1}/{p2}", method = RequestMethod.GET)
    public String hello2 (@PathVariable int p1, @PathVariable int p2, Model model){
        int result = p1 + p2;
        model.addAttribute("msg", result);
        return "test2";
    }

//    @RequestMapping("/hello2/{p1}/{p2}")
//    public String hello2 (@PathVariable int p1, @PathVariable String p2, Model model){
//        String result = p1 + p2;
//        model.addAttribute("msg", result);
//        return "test2";
//    }
}

 

posted @ 2020-06-18 15:45  梦想成为DALAO  阅读(178)  评论(0编辑  收藏  举报