Good time of the day
I have some problems in Spring boot
Situation: I am doing Update operation. My controller receiving the instance of object via service and adding to model.
In form, I want to change some attributes of object, and showing attributes via input tag, like
<input type="text" th:field="*{name}" >
Problem
The attributes not added to form becomes null after submitting the form. While all attributes included on form showing the new values or old ones after submitting.
My Controller
@GetMapping("/recipe/{recipeId}/ingredient/{id}/edit")
public String editRecipeIngredient(@PathVariable String recipeId, @PathVariable String id, Model model) {
IngredientCommand command = ingredientService.findByRecipeIdAndIngredientId(Long.valueOf(recipeId), Long.valueOf(id)); // instance of class
model.addAttribute("ingredient", command);
log.info("The recipe id before POST === " + command.getRecipeID());
model.addAttribute("uoms", unitOfMeasureService.findAllUoms());
return "recipe/ingredient/ingredientform";
}
@PostMapping
@RequestMapping("/recipe/{recipeId}/ingredient")
public String updateIngredient(@ModelAttribute IngredientCommand command) {
log.info("The recipe id after POST === " + command.getRecipeID());
log.info("Amount " + command.getAmount());
IngredientCommand savedIngredient = ingredientService.saveIngredientCommand(command);
return "redirect:/ingredi";
}
My form
<form class="form" th:object="${ingredient}" th:action="'/recipe/'+${ingredient.id}+'/ingredient'" method="post">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<input class="form-control" th:field="*{recipeID}">
</div>
<div class="form-group">
<label> Description
<input type="text" th:field="*{description}" class="form-control">
</label>
<label> Amount
<input type="number" th:field="*{amount}" class="form-control">
</label>
<label>Unif of Measure
<select name="" id="" class="form-control">
<option th:each="uom:${uoms}"
th:value="${uom.id}"
th:text="${uom.description}"
th:selected="${uom.id.equals(ingredient.uom.id)}">
Option
</option>
</select>
</label>
</div>
<div class="form-group mt-2">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
</form>
Is it normal behavior? Or maybe I can to not specify some attributes in form and save their values?
