Tell me if I understand correctly how Spring MVC works.
1. When I create a form and then accept it in the controller, I get the finished object of my class (those, unlike servlets, I do not need to take each attribute and substitute it into an object)? Do I understand the principle of work?
2. What is the difference between modelAttribute and commandName?

    2 answers 2

    1. Yes, that's right, this is called data binding . Spring retrieves data from the request and provides you with a ready object. Moreover, the use of the Spring TLD library (this is the one that provides you with form:* tags form:* ) is not required - it simply offers a more convenient way of writing when passing an object to the View. You can write forms on bare HTML and @ModelAttribute will work just as well.
    2. Nothing, CommandName is outdated, but apparently remains for backward compatibility. If you look at the code , it is the same object.

     /** * Set the name of the form attribute in the model. * <p>May be a runtime expression. * @see #setModelAttribute */ public void setCommandName(String commandName) { this.modelAttribute = commandName; } /** * Get the name of the form attribute in the model. * @see #getModelAttribute */ protected String getCommandName() { return this.modelAttribute; } 
    • Thanks for the answer, are those better to use modelAttribute? Could you write a sample code when processing is done on pure html and the object is transmitted? (I don’t really understand how he will collect him) - GermanSevostyanov
    • To figure out who is collecting what, try the following: create a test JSP page, where Spring Form tags will be used (which you asked for); run the project, go to this page and look at its source code in the browser (you will see in which HTML code the Spring displayed the form, this is called rendering); confirm the form and in the browser console look at the request that is sent to the server and pay attention to the names of the parameters - they can be the same as the names of the fields in the class marked @ModelAttribute@ - enzo
    • Are those really effortlessly using the jsp library on jsp to send an object? - GermanSevostyanov
    • Of course, because as a result, all these auxiliary tags form:* still converted to HTML. You can immediately write the same HTML code in JSP without using Spring tags. This library is just an auxiliary thing for writing less code. Someone likes, some do not. - enzo

    Right. commandName specifies the object to send to the controller. but if you specify in the attribute of the form instead of commandName - modelAttribute (personally, I) modelAttribute exception (but rather it is connected with the version of the spring).
    In addition, in one of the latest versions of the release of the spring, I recently found out that you can no longer implement separate classes (example: Class - ClassForm ), which were usually placed in separate packages, and create a method in the controller itself, in which we simply return the filled entiti :

     @ModelAttribute(name="entity") public Entity getEntity() { return new Entity(); } @RequestMapping({value}) public String someMethod(@ModelAtrribute("entity") Entity entity) {} 

    A jsp spring defines the name specified in the annotation parameter as commandName .

    • Thanks a lot for the answer :) - GermanSevostyanov