简体   繁体   中英

Spring @ModelAttribute doesn't care about commandName

JSP:

<form:form commandName="editWeather" method="post" action="../edit">
    <!-- Input fields -->
    <input type="submit" value="Submit">
</form:form>

And this is how I get the model in Spring:

@ModelAttribute("DONTGIVEADAMN") Weather weather

And I can still use the weather to do my operations and it works great, for example:

weatherService.editWeather(weather);

My question is...Why does this work?

Model attribute name doesn't matter when binding data received from a form (because names of form fields correspond to the names of fields of the model object), it matters only when rendering a form.

I particular, when model attribute name in your POST handler method doesn't match the commandName in the form, you will be able to receive the data, but won't be able to redisplay a form with validation errors.

its matching the class type (or interface), not the name of the variable/parameter; and the specified request mapping/method signature must be correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM