Spring MVC 4: Introduction to method input parameters

Source: Internet
Author: User

In the Spring MVC framework, you can define the input parameters of the Request Processing Method in any order (except that Errors and BindingResult must be followed by the command object/form parameters ), spring MVC automatically transmits the corresponding object to the request processing method through the input parameter based on the reflection mechanism. This mechanism allows developers to develop control layer programs without relying on Servlet APIs. When request processing methods require specific objects, they only need to declare input parameters in the parameter list, you do not need to consider how to obtain these objects. The following lists the processing method parameters supported by spring mvc. * By default, the basic data types of Java and String are bound to URL parameters by name, you can use the @ RequestParam annotation to change the default binding rule * Request or response objects (Servlet API ). request or response object (Servlet API), select any request or response type, such as: ServletRequest, HttpServletRequest. * Session object (Servlet API ). session Object (Servlet API): The composition type includes HttpSession. This type of parameter enhances the session of the current communication. Therefore, this parameter will never be null. note: The session channel is not thread-safe, especially in the Servlet environment. If multiple requests are allowed to access the session, it is recommended that the Annot Set the synchronizeOnSession attribute of ationMethodHandlerAdapter to "true" * org. springframework. web. context. request. webRequest or org. springframework. web. context. request. nativeWebRequest. allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API. webRequest or NativeWebRequest encapsulated by spring is comparable to the native Servlet/Portlet API. *. Java. util. Locale is used to obtain the current request region. *. Java. io. inputStream/java. io. reader for access to the request's content. this value is the raw InputStream/Reader as exposed by the Servlet API. the value is the original InputStream/Reader. The Servlet API can access the request content *. java. io. outputStream/java. io. writer for generating the response's content. this value is the raw OutputStream/Writer as exposed by the Servlet API. this operation is used to generate the response content *. java. security. P Rincipal containing the currently authenticated user. Contains the currently authenticated user. Spring 3.0 adds a @ PathVariable annotation to support variable request paths *. @ PathVariable annotated parameters for access to URI template variables is used to annotate the variable parameters corresponding to the address bar. for example: Java code @ RequestMapping (value = "/owners/{ownerId }", method = RequestMethod. GET) public String findOwner (@ PathVariable ("ownerId") String ownerId, Model model) {// implementation omitted} access address "/owners/{ownerId}" specifies the access variable name as ownerId. When the controller processes this request, the ownerId value is set to the request address bar, for example: When the request comes from/owners/fred, the value of fred is bound to the parameter String ownerId * of the access method *. @ RequestParam is used to annotate parameters to Servlet request parameters. The parameter values correspond to the parameters declared in the Controller method. Java code @ RequestMapping (method = RequestMethod. GET) public String setupForm (@ RequestParam ("id") int petId, ModelMap model) {Pet pet = this. clinic. loadPet (petId); model. addattriform ("pet", pet); return "petForm";} For example, the request parameter id corresponds to the petId in the method. *. @ RequestHeader the annotation parameter is used for the http header of a specific Servlet request. The parameter value is converted to the method parameter type of life. Request Header information such as Connection, Accept-Language, Accept-Charset and other Java code public void displayHeaderInfo (@ RequestHeader ("Connection") String connection, @ RequestHeader ("Accept-Encoding ") string encoding) {System. out. println ("connection:" + connection); System. out. println ("Accept-Encoding:" + encoding );}*. @ RequestBody *. httpEntity <?> *. Java. util. map/org. springframework. ui. model/org. springframework. ui. modelMap binds the potential model objects created by each request in the Spring MVC framework. They can be accessed by Web View objects (such as JSP ). it is used to enhance the model passed to the web View page. That is to say, after they are used as method parameters, the page view can directly obtain the corresponding value based on the key. *. Org. springframework. validation. errors/org. springframework. validation. the BindingResult is inherited from Errors. It is the verification result of the command/form object in the attribute list. An error message is returned to the page. After setting it as a method parameter, you can directly obtain it on the view page. BindingResult result must be used together with @ ModelAttribute, and parameters must be kept in touch. Otherwise, an error is reported. Errors/BindingResult argument declared without preceding model attribute is abnormal. Example 1 and 2: An error is reported; 3 is normal. // Test: 1 Java code @ RequestMapping (params = "method = result1") public String processSubmit1 (User user User, Model model, BindingResult result) {return "/demo1/listBoard";} // test: 2 Java code @ RequestMapping (params = "method = result2") public String processSubmit2 (@ ModelAttribute ("user ") user user, Model model, BindingResult result) {return "/demo1/listBoard";} // test: 3 Java code @ RequestMapping (params = "method = res Ult3 ") public String processSubmit3 (@ ModelAttribute (" user ") User user, BindingResult result, Model model) {return"/demo1/listBoard ";} * org. springframework. web. bind. support. sessionStatus status handle for marking form processing as complete, which triggers the cleanup of session attributes that have been indicated by the @ SessionAttributes annotation at the handler type level. you can use this type of status object to display This is equivalent to triggering the session to clear the attributes defined by @ SessionAttributes. For example, you can use SessionStatus to control the model attribute at the Session level. Java code @ RequestMapping (method = RequestMethod. POST) public String processSubmit (@ ModelAttribute Owner owner, BindingResult result, SessionStatus status) {// <-- ① new OwnerValidator (). validate (owner, result); if (result. hasErrors () {return "ownerForm";} else {this. clinic. storeOwner (owner); status. setC Omplete (); // <-- ② return "redirect:/owner. do? OwnerId = "+ owner. the owner form object in the getId () ;}} processSubmit () method will be bound to the "owner" attribute of ModelMap. The result parameter is used to store the object that verifies the owner result, status is used to control the status of form processing. In section ②, we call the status. setComplete () method to clear all model attribute data of the Controller at the session level from the session.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.