Help understand this spring boot code (make it better too)

@Controller
public class CategoriesController {
@Autowired
private CategoryService categoryService;

@GetMapping(value = "/admin/categories")
public String editCategories(Model model) {
model.addAttribute("category", new Category());
return "editors/categories";
}

@PostMapping(value = "/admin/categories")
public String addCategory(@Valid @ModelAttribute(name = "category") Category category, Errors errors, Model model, RedirectAttributes redirectAttributes) {
if (errors.hasErrors()) {
model.addAttribute("category", category);
return "editors/categories";
}
categoryService.save(category);
redirectAttributes.addFlashAttribute("successMessage", "Added category successfully");
return "redirect:/admin/categories";
}

@ModelAttribute
public void addCommonAttributes(Model model, HttpServletRequest request) {
model.addAttribute("categories", categoryService.getAllCategories());
model.addAttribute("category", new Category());
model.addAttribute("requestURI", request.getRequestURI());
}
}
@Controller
public class CategoriesController {
@Autowired
private CategoryService categoryService;

@GetMapping(value = "/admin/categories")
public String editCategories(Model model) {
model.addAttribute("category", new Category());
return "editors/categories";
}

@PostMapping(value = "/admin/categories")
public String addCategory(@Valid @ModelAttribute(name = "category") Category category, Errors errors, Model model, RedirectAttributes redirectAttributes) {
if (errors.hasErrors()) {
model.addAttribute("category", category);
return "editors/categories";
}
categoryService.save(category);
redirectAttributes.addFlashAttribute("successMessage", "Added category successfully");
return "redirect:/admin/categories";
}

@ModelAttribute
public void addCommonAttributes(Model model, HttpServletRequest request) {
model.addAttribute("categories", categoryService.getAllCategories());
model.addAttribute("category", new Category());
model.addAttribute("requestURI", request.getRequestURI());
}
}
3 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
userexit
userexitOP3w ago
on the /admin/categories i add the attribute category in the model this is useless i already do this in @ModelAttribute which makes sure every request has a new Category but then in the PostMapping I add the category again if there are errors that shouldnt work right ?
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?