Tag: Val constructor show AMs Path exists local BSP long
First, preface
In the previous section, we configured the annotation driver and the automatic scan package, then we can use the SPRINGMVC related annotations.
Second, @Controller
@Controller used to modify the class, the source code is as follows:
Package Org.springframework.stereotype;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target; @Target ({elementtype.type}) @Retention ( Retentionpolicy.runtime) @Documented @componentpublic @interface Controller { String value () Default "";}
View Code
(1) Modified by @component, indicating that this annotation is used to identify a bean.
(2) Used to indicate that the class being modified is the controller class.
Third, @RequestMapping1. Property detailed
@RequestMapping annotations are used to decorate a class or method with the following source code:
Package Org.springframework.web.bind.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target; @Target ({Elementtype.method, Elementtype.type}) @Retention (retentionpolicy.runtime) @Documented @mappingpublic @interface requestmapping { String[] Value () default {}; Requestmethod[] Method () default {}; String[] Params () default {}; String[] Headers () default {}; String[] Consumes () default {}; String[] produces () default {};}
View Code
(1) value
The Value property maps the request URL (such as "/hello") to the entire class or to a specific processing method.
The Value property is the default property of Requestmapping, meaning that @RequestMapping (value = "/hello") can be written as @RequestMapping ("/hello").
(2) method
The method property is used to specify the following request method: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
2. Annotation simplification
@RequestMapping (value= "/hello", method=requestmethod.get) can be simply written @GetMapping ("/hello")
Similar annotations include:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
Four, parameter bean
Spring will automatically inject its bean object for the following types of parameters.
See: Https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments
Five, method parameters
The following annotations can be used to decorate the request parameters.
1.@RequestParam
Request parameters can be received via Requestparam
@Controller @requestmapping ("/pets") public class Editpetform { //... @GetMapping public String setupform (@RequestParam ("Petid") int petid, model model) { Pet pet = This.clinic.loadPet (Petid); Model.addattribute ("Pet", pet); return "Petform"; } // ...}
View Code
[email protected]
This annotation allows you to obtain information about the request header.
For example, a request header is:
Host localhost:8080Accept text/html,application/xhtml+xml,application/xml;q=0.9Accept -language fr,en-gb;q=0.7,en;q=0.3Accept-Encoding gzip,deflateaccept-charset iso-8859-1,utf-8;q=0.7,*;q=0.7Keep-alive 300
You can get the request header information in the following way
@GetMapping ("/demo")publicvoid handle ( @RequestHeader ("accept-encoding") ) String encoding, @RequestHeader (long keepAlive) { //...}
View Code
3.
@CookieValue
Get cookies
@GetMapping ("/demo")publicvoid handle (@CookieValue ("Jsessionid") String cookie) { // ...}
View Code
[Email protected]
PathVariable
Path variables can be received via @pathvariable
@Controller @requestmapping ("/owners/{ownerid}") public class Ownercontroller { @GetMapping ("/pets/{petid}") Public Pet Findpet (@PathVariable long ownerid, @PathVariable long Petid) { //... }}
View Code
[Email protected]
ModelAttribute
The @modelattribute that is labeled on the method parameter shows that the value of the method parameter is obtained from the model. If it is not found in the model, the parameter is instantiated first and then added to the model. After the model is present, all the name matching parameters in the request are populated into the parameter.
@PostMapping ("/owners/{ownerid}/pets/{petid}/edit") public String processsubmit (@ModelAttribute Pet pet) {}
View Code
As an example of the above code, where might this instance of the pet type come from? There are several possibilities:
- It may be because the use of @sessionattributes annotations already exists in model
- It may be that the @modelattribute method used in the same controller already exists in the model-as described in the previous section
- It may be obtained from URI template variables and type conversions (explained in more detail below)
- It is possible that the default constructor that called itself is instantiated.
SPRINGMVC_ Summary _03_SPRINGMVC related annotations