Home > Back-end >  In the model of object seems to be lost
In the model of object seems to be lost

Time:02-12

Recently in "Sring actual combat", see chapter 2, section 3 in the wrong,
With the Spring Tool Suite to create a project, page templates with thymeleaf,
Java tacos. Web. DesignTacoController.
 package tacos. Web; 
The import... ;//omission is not important code
The import tacos. Taco;
The import tacos. Ingredient;
The import tacos. Ingredient. Type;
@ Slf4j
@ Controller
@ RequestMapping ("/design ")
Public class DesignTacoController {
@ GetMapping
Public String showDesignForm Model (Model) {
List Ingredients=Arrays. AsList (
New Ingredient (" FLTO ", "Flour Tortilla", the WRAP)...//omission is not important code
);
Type [] types=Ingredient. The Type. The values ();
For (Type, Type: types) {
Model. The addAttribute (type. The toString () toLowerCase (), filterByType (ingredients, type));
}
Model. The addAttribute (" design ", the new Taco ());//here in the model into called design Taco Bean
Return "design";
}

@ PostMapping
Public String processDesign (@ Valid Taco design, Errors Errors) {//here seems to be caused by the error, will @ Valid after remove, is not an error
{if (errors. HasErrors ())
Return "design";
}
The info (" Processing design: "+ design);
The return of "redirect:/orders/current";
}

Private List FilterByType (List Ingredients, Type, Type) {... }//omit important code
}

Java tacos. Taco.
 package tacos. 
The import... ;
@ Data
Public class Taco {
@ NotNull
@ the Size (min=5, the message="the Name must be at further 5 character long")
private String name;
@ NotNull
@ the Size (min=1, the message="You must choose the at further 1 ingredient")
Private List Ingredients;
}

The design. The HTML
 & lt; ! DOCTYPE html> 
XMLNS: th="http://www.thymeleaf.org" & gt;

Taco Cloud

<body>
.




.








Visit http://localhost:8080/design for the first time won't be an error, but if the form to fill in the data is not in conformity with the requirements, such as not choose any a check box or did not fill in the name, an error when you submit the form and below is some information may be useful error:
(1) the 2021-02-11 23:52:52. 551 ERROR 18440 - [nio - 8080 - exec - 1]. Org. Thymeleaf TemplateEngine: [thymeleaf] [HTTP - nio - 8080 - exec - 1] Exception processing template "design" : An ERROR happened during the template parsing (template: "class path resource/templates/design. The HTML")
(2) under Caused by: org. Attoparser. ParseException: Error during the execution of processor '. Org. Thymeleaf spring5. Processor. SpringInputGeneralFieldTagProcessor '(template: "design" - the line 58, col 28), note: here's the line 58, col 28 or above I in the design in the HTML mark the location of an Error here,
(3) : under Caused by: Java. Lang. An IllegalStateException: Neither BindingResult nor plain target object for bean name 'design' available as request attribute, here is what I said Taco bean missing error,
When using the GET method showDesignForm (), I asked in the model in the called design Taco Bean, submit the form, because the form is not set the action attribute, the request is still sent to the/design, if the submitted form data is not in conformity with the requirements, will trigger the error, visit/design again, otherwise you will be redirected to the/orders/current, can use POST method to access/design again, but the report is called the Taco design Bean,
What is wrong? In the model of the object is valid for how long? Two requests can't access each other data in the model?
-- -- -- -- -- -- -- -- 20210212 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Go back and read "Spring of actual combat," to see so a words: "the Model object is responsible for transfer data between controller and display data view, in fact, in the Model attribute of the data will be copied to the Servlet in the Response attribute, so the view can find them here", now probably understand the cause of the error, the Model of object data is copied to the Servlet Response, so the Model of the object lifetime and Servlet Response object's lifetime is the same, is only effective in a request, so in a request of the things in the Model, the next time should be can not access, but here is strange, why in the Model of the Taco Bean why multiple access?
  • Related