1. Controller
- Controller replication provides access to the behavior of the application, usually through interface definition or annotation definition
- The controller is responsible for parsing the user’s request and transforming it into a model
- A controller can contain multiple methods in SpringMVC
- In SpringMVC, there are several ways to configure controllers
2. RequestMapping
RequestMapping: a method used to map a URL to a controller class or a specific handler
-
Add only to methods at localhost/ project name /t1
@Controller public class TestController { @RequestMapping("/t1") public String test(a){ return "test"; }}Copy the code
-
Localhost/project name /test/t1: localhost/ project name /test/t1: localhost/ project name /test/t1
@Controller @RequestMapping("/test") public class TestController { @RequestMapping("/t1") public String test(a){ return "test"; }}Copy the code
3. The RESTful style
3.1 concept
RestFul is a style of resource location and operation. Software designed based on this style can be more concise, more hierarchical, and easier to implement caching mechanisms
3.2 features
- ** Resources: ** All transactions on the Internet can be called resources
- ** Resource operations: ** Use POST, GET, DELETE, and PUT to perform operations on resources
3.3 Traditional Resource Operations
- Through different parameters to achieve different effects, the method is single, namely 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 updates, the POST
- Delete the http://127.0.0.1/item/deleteItem.action?id=1 GET or POST
3.4 RESTful Operation resources
- Different requests can be made to achieve different effects, with the same address but different functionality
- http://127.0.0.1/item/1 query, GET
- http://127.0.0.1/item new POST
- http://127.0.0.1/item updates, the PUT
- http://127.0.0.1/item/1 DELETE, DELETE
3.5 using a RESTful
-
Create a new Controller
@Controller public class RestFulController {}Copy the code
-
In SpringMVC, you can use the @pathVariable annotation to bind the value of a method parameter to a URI template variable
@Controller public class RestFulController { // Map the access path @RequestMapping("/commit/{p1}/{p2}") public String index(@PathVariable int p1, @PathVariable int p2, Model model){ int result = p1+p2; //Spring MVC automatically instantiates a Model object to pass values to the view model.addAttribute("msg"."Result:"+result); // Returns the view location return "test"; }}Copy the code
-
test
3.6 Benefits of using path variables
- Make the path more concise
- It is easier to get the parameters, and the framework automatically converts them
- Access parameters can be constrained by the type of the path variable. If the type is different, the corresponding request method cannot be accessed
4. Specify the request type using the method attribute
Used to constrain the type of request and narrow the request. Specifies the type of the request predicate. For example, GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, and TRACE
Testing:
-
Add a method
// Map access path, must be POST request @RequestMapping(value = "/hello", method = {RequestMethod.POST}) public String index2(Model model){ model.addAttribute("msg"."hello!"); return "test"; } Copy the code
-
If the browser is used to access the vm through GET, error 405 is reported
-
Change POST to GET to access normally
// Map access path, must be POST request @RequestMapping(value = "/hello", method = {RequestMethod.GET}) public String index2(Model model){ model.addAttribute("msg"."hello!"); return "test"; } Copy the code
- Conclusion:
- All address bar requests default to GET requests
- Method-level annotations are as follows: composite annotations
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping