SPRINGMVC the process of determining the target method Pojo type entry

Source: Internet
Author: User

SPRINGMVC determines the procedure access method of the target method Pojo type into the parameter    @RequestMapping ("/pojoparam") public    String pojoparam (user user) {                Return "Success";    } 1, first determine a key value ① if the Pojo type parameter of the target method is not using @modelattribute as the entry parameter, the key is the first lowercase example of the Pojo class name    Test (user user) {} This method key is User② if @modelattribute is used to decorate the parameter, then the key is the value of the @modelattribute note,    example test (the @ModelAttribute (value= " Users ") {} This method key is Users2, in the model to find key corresponding to the object, if present, then as the input parameter passed    in if the method in the @modelattribute tag has saved the key object in the map, Gets to 3, if it does not exist, checks whether the class is @sessionattributes annotated, if the annotation is used, the value in this annotation contains key, then the object of the key corresponding to the httpsession is obtained, if any, is directly passed into the parameter of the target method, if it does not exist, throws an exception 4, if the class is not @sessionattributes annotated, or the value of the annotation does not contain a key, the Pojo type of parameter is created by reflection, passed as the target method parameter 5, SPRINGMVC will save the key and the Pojo type object into the model and go to the Save to request

Case of Exception thrown: (Omitted in the Guide section)

/** * Model does not query the user, and there is @sessionattributes (value= "user"), there is a value=user, but there is no user object in the session domain, will throw an exception */@ Sessionattributes (value= "User") @Controllerpublic class Modelandviewaction {@RequestMapping ("/pojoparam") public String pojoparam (user user) {return "Success";}}

Thrown exception:

org.springframework.web.HttpSessionRequiredException:Session attribute ' user ' required-not found in Session

Use the @modelattribute () annotation notation method to put the user object into the model in advance

@SessionAttributes (value= "User") @Controllerpublic class Modelandviewaction {@ModelAttribute ("user") public user GetUser () {Family family=new Family (); Family.setcity ("Shanghai"); User User=new User (1, "Zhang San Ah", 12,family); return user;} @RequestMapping ("/pojoparam") public String pojoparam (user user) {return "Success";}}

This will not be an exception, the user object exists in the session and request domains, and the key is "user"

Methods for @ModelAttribute () annotations are called by SPRINGMVC before each target method executes

SPRINGMVC the process of determining the target method Pojo type entry

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.