🏇
Small
wood
come
Yes
\textcolor{Orange} {here comes Koki}
Here comes Koki
🍣
of
front
Yes
solution
Yes
S
p
r
i
n
g
M
V
C
of
primary
reason
,
notes
solution
open
hair
and
@
C
o
n
t
r
o
l
l
e
r
\textcolor{green} {learned about the principle of spring MVC, annotation development and @ Controller}
Previously, I learned the principle, annotation development and @ Controller of spring MVC 🍣
🍣
this
day
of
order
mark
yes
R
e
q
u
e
s
t
M
a
p
p
i
n
g
and
R
e
s
t
F
u
l
wind
grid
\textcolor{green} {today's goal is RequestMapping and RestFul style}
Today's goal is RequestMapping and RestFul style 🍣
🙏
Bo
main
also
stay
learn
Learn
rank
paragraph
,
as
if
hair
present
ask
topic
,
please
Tell
know
,
wrong
often
sense
thank
\textcolor{Orange} {blogger is also in the learning stage. If you find any problems, please let me know. Thank you very much}
Bloggers are also in the learning stage. If you find any problems, please let us know. Thank you very much 💗
The code used can be found here
5, RequestMapping
@The RequestMapping annotation is used to map URLs to a controller class or a specific handler method. Can be used on classes or methods. Used on a class to indicate that all methods in the class that respond to requests take this address as the parent path.
-
Annotations on classes and methods
(you can access the class first and then the method)
- Annotation class only
@Controller public class ControllerTest3 { @RequestMapping("/sss") public String test(){ return "test"; } }
Usually write it directly
6, RestFul style
concept
Restful is a style of resource location and resource operation. It's not a standard or agreement, it's just a style. The software designed based on this style can be more concise, more hierarchical, and easier to implement caching and other mechanisms.
Modify the item to find the person you need
function
Resources: all things on the Internet can be abstracted as resources
Resource operation: use POST, DELETE, PUT and GET to operate resources using different methods.
Add, delete, modify and query respectively.
- Operate resources in the traditional way: achieve different effects through different parameters! Single method, post and get
http://127.0.0.1/item/queryItem.action?id=1 Query, GET
http://127.0.0.1/item/saveItem.action New, POST
http://127.0.0.1/item/updateItem.action Update, POST
http://127.0.0.1/item/deleteItem.action?id=1 Delete, GET or POST
- Using RESTful operation resources: different effects can be achieved through different request methods! As follows: the request address is the same, but the function can be different!
http://127.0.0.1/item/1 Query, GET
http://127.0.0.1/item New, POST
http://127.0.0.1/item Update, PUT
http://127.0.0.1/item/1 DELETE
Learning test
In Spring MVC, you can use the @ PathVariable annotation to bind the value of the method parameter to a URI template variable.
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 RestFulController { //The original method: localhost: 8080 / add? A = 1 & B = 2 @RequestMapping("/add") public String test(int a, int b, Model model){ int res = a + b; model.addAttribute("msg","The result is" + res); return "test"; } //RestFul: localhost:8080/add2/1/2 @RequestMapping("/add2/{a}/{b}") public String test1(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","The result is" + res); return "test"; } }
thinking Test : send use road path change amount of good place ? \textcolor{orange} {thinking: the benefits of using path variables?} Think: what are the benefits of using path variables?
- Make the path more concise;
- Efficient and safe
- It is more convenient to obtain parameters, and the framework will automatically perform type conversion.
- Access parameters can be constrained by the type of path variables. If the types are different, the corresponding request method cannot be accessed. For example, if the access path here is / commit/1/a, the path does not match the method, rather than parameter conversion failure.
Use the method property to specify the request type
It is used to constrain the type of request and narrow the request range. Specify the type of request predicate, such as GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, etc
-
post
//The mapped access path must be a POST request @RequestMapping(value = "/add/{a}/{b}",method = {RequestMethod.POST}) public String test3(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","The result is" + res); return "test"; }
It is OK to jump from this jsp page through the submit button
-
We use the browser address bar to access. By default, it is a Get request, and an error 405 will be reported:
If you change POST to GET, it is normal;
//Map access path, must be a Get request @RequestMapping(value = "/add/{a}/{b}",method = {RequestMethod.GET}) public String test2(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","The result is" + res); return "test"; }
Summary:
The @ RequestMapping annotation of Spring MVC can handle HTTP requests, such as GET, PUT, POST, DELETE and PATCH.
All address bar requests will be of HTTP GET type by default.
Method level annotation variants are as follows: combined annotation
@GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping
@GetMapping is a combined annotation, which is often used!
It acts as a shortcut to @ RequestMapping(method =RequestMethod.GET).