牛客论坛项目day1|spring MVC入门

8.11周五

1、从网页获取数据

  • GET:主要用于获取数据。
  • POST:主要用于创建或更新资源。

get请求的两种传参方法

1、

//get请求
// /students?current=1&limit=20
@RequestMapping(path = "/students", method = RequestMethod.GET)
@ResponseBody
public String getStudents(
        @RequestParam(name = "current", required = false, defaultValue = "1") int current,
        @RequestParam(name = "limit", required = false, defaultValue = "20")int limit
){
    System.out.println(current);
    System.out.println(limit);
    return "some students";
}

2、

// /student/123
@RequestMapping(path = "/student/{id}", method = RequestMethod.GET)
@ResponseBody
public String getStudent(
        @PathVariable(name = "id") int id
){
    System.out.println(id);
    return "a student";
}

post请求

//post请求
@RequestMapping(path="/student", method = RequestMethod.POST)
@ResponseBody
public String saveStudent(String name, int age){
    System.out.println(name);
    System.out.println(age);
    return "success";
}

2、发送数据

响应一个html数据

你可能感兴趣的:(spring,mvc,java)