I have a small project where i can add categories to a table in my SQL database. I have another table called products. In the "/addProducts" screen i want a drop down to appear in my form. I would like the dropdown to be populated from the data in the "Categories" table from my database. But at the moment, I am unable to do it.
So if anyone could help me with some thymleaf/html code to show the data in my form i would appreciate it.
Any advice is greatly appreciated.
PS i am not using a DAO class nor do i wish to hardcode the values. Simply would like to be able to fetch the data directly from the database and display it in a dropdown on a form. Thanks :
1 CategoryController
@Slf4j
@Controller
public class CategoryController implements Serializable {
private CategoryRepository categoryRepository;
public CategoryController(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}
@Autowired
CategoryRepository service;
@Autowired
private UserRepository userRepository;
@GetMapping("/category")
public String displayCategory(Model model) {
model.addAttribute("category", service.findAll());
return "/category";
}
@GetMapping("/categoryUserView")
public String displayCategoryUser(Model model) {
model.addAttribute("category", service.findAll());
return "/categoryUserView";
}
@PostMapping("/addCategory")
public String processOrder(@Valid Category category, BindingResult result, SessionStatus
sessionStatus, Model model) {
if (result.hasErrors()) {
return "addCategory";
}
service.save(category);
model.addAttribute("category", service.findAll());
return "category";
}
@GetMapping("/editCategory/{id}")
public String showUpdateCategoryForm (@PathVariable("id") long id, Model model){
Category category = service.findAllById(id);
model.addAttribute("category", category);
return "editCategory";
}
@PostMapping("/updateCategory/{id}")
public String updateFixtures ( @PathVariable("id") long id, @Valid Category category,
BindingResult result, Model model){
if (result.hasErrors()) {
category.setId((int) id);
return "editFixtures";
}
service.save(category);
model.addAttribute("category", service.findAll());
return "redirect:/category";
}
@GetMapping("/deleteCategory/{id}")
public String deleteCategory(@PathVariable("id") long id, Model model) {
Category category = categoryRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
categoryRepository.delete(category);
model.addAttribute("category", categoryRepository.findAll());
return "redirect:/category";
}
}
ProductController
@Slf4j
@Controller
public class ProductController implements Serializable {
private ProductRepository productRepository;
public ProductController(ProductRepository productRepository) {
this.productRepository = productRepository;
};
@Autowired
ProductRepository service;
@Autowired
private UserRepository userRepository;
@GetMapping("/products")
public String displayCategory(Model model) {
model.addAttribute("product", service.findAll());
return "/product";
}
@GetMapping("/addProduct")
public String showSignUpForm(SessionStatus sessionStatus,
@AuthenticationPrincipal User user, Product product, Model
model) {
model.addAttribute("category",service.findAll());
return "addProduct";
}
@PostMapping("/addProduct")
public String processOrder(@Valid Product product, BindingResult result, SessionStatus
sessionStatus, Model model) {
if (result.hasErrors()) {
return "addProduct";
}
//category.setUser(user);
service.save(product);
model.addAttribute("product", service.findAll());
return "product";
}
}
Category Model
@Data
@Entity
@Table(name = "product_categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_category_id")
private long id;
@Column(name = "category_name")
private String name;
@Column(name = "category_description")
private String desc;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Product Model
@Data
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "product_id", referencedColumnName = "product_category_id")
public Category category;
@Column(name = "product_name")
private String name;
@Column(name = "producy_description")
private String desc;
@Column(name = "producy_quanitity")
private int quantity;
@Column(name = "producy_price")
private double price;
}
}
AddProducts HTML
<h1> Add your Products Here</h1>
<form method="POST" th:action="@{/addProduct}" class="form-
container" id="aboutForm">
<div class="form-group col-md-8">
<label class="col-form-label">Category </label>
<select id="category" name="category" th:field="*{category.id}" >
<option th:each="category : ${category}" th:value="${category.id}"
th:utext="${category.name}"/>
</select>
</div>
</form>
Error from HTMl
Bean property 'id' is not readable or has an invalid getter
method:
Does the return type of the getter match the parameter type