ResuFul风格

ResuFul风格

有时我们访问一个网页需要传递参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.dwx.controller;


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


@Controller
public class MyController {

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

无参时的情况

如果没有传递参数那么就会报500的错误

如何传参?

在以前的情况直接使用?参数=xx就能完成传参

1
http://localhost:8080/s/h2?a=1&b=3

之前的传参方法但是这种传参方法有个很大的问题:必须知道参数名,很不安全,有SQL注入问题

而且这种情况也使得命令行看起来很凌乱

RestFul风格

此时,如果我们使用下边这种方式,就能通过** \ **来传递参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.dwx.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
public class MyController {

@RequestMapping("/hello")
public String helloServlet(Model model){
model.addAttribute("msg","hello SpringMvc-annotation");
return "hello";
}

@RequestMapping("/h2/{a}/{b}")
public String Servlet(@PathVariable int a, @PathVariable int b, Model model){
int rest;
rest = a + b;
model.addAttribute("msg","结果为"+rest);
return "hello";
}
}

1
http://localhost:8080/s/h2/2/3

restful

这就是我理解的RestFul风格

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信