I am wondering how I could check the RequestParams to see if they are empty so I could forward to another html page, right now I am just working around this by checking if each parameter is empty. The problem is I want to forward to another page if the user tries to submit a compeletly empty form and if they do that my program will crash. It will crash because the program tries to convert the int Price to a string becasue that is what is submitted in the empty form. I know I could just make the Int Price a String but I was wondering if there was another soultion.
@Controller
public class HomeController {
@Autowired
private GlassesList glassesList;
@GetMapping("/")
public String goHome(Model model) {
return "index";
}
@GetMapping("/addGlasses")
public String addSunglasses(Model model) {
return "addGlasses";
}
@GetMapping("/searchGlasses")
public String searchSunglasses() {
return "searchGlasses";
}
@GetMapping("/displayGlasses")
public String displaySunglasses() {
return "displayGlasses";
}
@PostMapping("/formPost")public String processForm (
Model model,
@RequestParam String productName,
@RequestParam String brandName,
@RequestParam String frameType,
@RequestParam String color,
@RequestParam String size,
@RequestParam String lensType,
@RequestParam int price
) {
if (productName.equals("") && brandName.equals("") && frameType.equals("") && color.equals("") && size.equals("") && lensType.equals("") && price == 0) {
return "displayGlasses";
}
else {
Glasses glasses = new Glasses(productName, brandName,frameType,
color, size, lensType, price);
glassesList.getGlassesList().add(glasses);
model.addAttribute("glassesList", glassesList);
System.out.println(model.getAttribute(lensType));
System.out.print(glassesList.getGlassesList());
return "addGlasses";
}
}
@ModelAttributeinstead, and you could write a clean method on that class.