Recommended tutorial: "java video tutorial"
What are the 5 commonly used annotations in springmvc?
The 5 commonly used annotations in springmvc are:
1. Component annotations:
1. @Component Add @ before the class definition Component annotation will be recognized by the spring container and converted into a bean.
2. @Repository annotates the Dao implementation class (special @Component)
3. @Service is used to annotate the business logic layer, (special @Component)
4. @Controller is used for control layer annotations, (special @Component)
The above four annotations are all annotated on the class, and the annotated class will be initialized by spring as a bean. Then manage them uniformly.
2. Request and parameter type annotations:
1, @RequestMapping: used to process request address mapping, can be applied to classes and methods superior.
●value: Define the mapping address of the request request
●method: Define the method of requesting the address, including [GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. 】By default, get requests are accepted. If the request method is different from the defined method, the request will not succeed.
●params: Define the parameter values that must be included in the request request.
●headers: Define that the request request must contain certain specified request headers, such as: RequestMapping(value = "/something", headers = "content-type=text/*") indicates that the request must contain A matching request is a Content-type header containing "text/html", "text/plain".
●consumes: Define the type of content requested to be submitted.
●produces: Specify the content type to be returned. Only when the (Accept) type in the request header contains the specified type will it be returned
@RequestMapping(value="/requestTest.do",params = {"name=sdf"},headers = {"Accept-Encoding=gzip, deflate, br"},method = RequestMethod.GET) public String getIndex(){ System.out.println("请求成功"); return "index"; }
The above code indicates that the request method is a GET request. The request parameters must include the parameter name=sdf, and then the request header must have the type header Accept-Encoding=gzip, deflate, br.
In this way, a request can be constrained through annotations.
2.@RequestParam: used to obtain the value of the incoming parameter
●value: the name of the parameter
●required: definition Whether the incoming parameter is required, the default is true, (similar to the params attribute of @RequestMapping)
@RequestMapping("/requestParams1.do") public String requestParams1(@RequestParam(required = false) String name){ System.out.println("name = "+name); return "index"; } @RequestMapping("/requestParams2.do") public String requestParams2(@RequestParam(value = "name",required = false) String names){ System.out.println("name = "+names); return "index"; }
The two methods of inputting parameters are the same. When the name of the declared value is displayed, the parameter name and value are entered. Same, if there is no explicit declaration, as declared in the first way, the input parameter names and function parameter variable names will be the same.
3.@PathViriable: used to define the path parameter value
●value: the name of the parameter
●required: defines whether the incoming parameter is Required value
@RequestMapping("/{myname}/pathVariable2.do") public String pathVariable2(@PathVariable(value = "myname") String name){ System.out.println("myname = "+name); return "index"; }
This path declares {myname} as a path parameter, then this path will have any value, and @PathVariable will be able to obtain the value of the path based on value.
4.@ResponseBody: Acts on the method, and the entire return result can be returned in a certain format, such as json or xml format.
@RequestMapping("/{myname}/pathVariable2.do") @ResponseBody public String pathVariable2(@PathVariable(value = "myname") String name){ System.out.println("myname = "+name); return "index"; }
It does not return a page, but prints the string "index" directly on the page. This is actually similar to the following code.
PrintWriter out = resp.getWriter(); out.print("index"); out.flush();
5, @CookieValue: Used to obtain the requested Cookie value
@RequestMapping("/requestParams.do") public String requestParams(@CookieValue("JSESSIONID") String cookie){ return "index"; }
6. @ModelAttribute:
is used to save parameters into the model. You can annotate methods or parameters. When annotated on a method, the method will be executed in the processor method. Execute before, and then store the returned object in the session (the prerequisite is @SessionAttributes annotation) or model attribute. @ModelAttribute("attributeName") is specified when marking the method. If not specified, the class name of the return type is used. (lowercase first letter) as the attribute name.
@ModelAttribute("user") public UserEntity getUser(){ UserEntity userEntityr = new UserEntity(); userEntityr.setUsername("asdf"); return userEntityr; } @RequestMapping("/modelTest.do") public String getUsers(@ModelAttribute("user") UserEntity user){ System.out.println(user.getUsername()); return "/index"; }
In the above code, the @ModelAttribute("user") annotation is used, which is executed before executing the controller. Then a model data named user will be generated. In the controller, we annotate it on the parameters. @ModelAttribute gets the parameters, and then applies the model to the controller. We can also use it in the jsp page,
<body> ${user.username} </body>
7, @SessionAttributes
Default Next Spring MVC stores the data in the model into the request field. When a request ends, the data is invalid. If you want to use it across pages. Then you need to use session. The @SessionAttributes annotation allows a copy of the data in the model to be stored in the session domain. When used with @ModelAttribute("user"), the model value of the corresponding name will be stored in the session.
@Controller @RequestMapping("/test") @SessionAttributes(value = {"user","test1"}) public class LoginController{ @ModelAttribute("user") public UserEntity getUser(){ UserEntity userEntityr = new UserEntity(); userEntityr.setUsername("asdf"); return userEntityr; } @RequestMapping("/modelTest.do") public String getUsers(@ModelAttribute("user") UserEntity user ,HttpSession session){ System.out.println(user.getUsername()); System.out.println(session.getAttribute("user")); return "/index"; } }
Combined the code of the previous example, added the @SessionAttributes annotation, and then requested it twice , the value of the attribute named user does not exist in the first session. When requesting for the second time, it is found that there is another value in the session. This is because the model data has not been saved in the session during the first request. It is saved only after returning. The last model can be obtained during the second request.
Note: @ModelAttribute("user") UserEntity When the user obtains the annotation content, it will first query whether there is a corresponding attribute value in the session, and then query the Model if not.
Recommended related articles: "java Development Tutorial"
The above is the detailed content of What are the 5 commonly used annotations in springmvc?. For more information, please follow other related articles on the PHP Chinese website!