Make employeeId
as a required param to serve the request by this RequestMapping
then you don't need to write else part because Employee
model will never be null
in that case. It must have at least employeeId
.
Note: Make it POST
request.
Example:
@RequestMapping(value = "/insert", params = "employeeId", method = RequestMethod.POST)
public String insertEmpDetails(@ModelAttribute("employee") Employee emp) {
empService.insertEmpDetails(emp);
return "redirect:/getList";
}
In other way you can validate the model and redirect to form page back with validation error messages.
Sample:
@RequestMapping(value = "/insert", params = "employeeId", method = RequestMethod.POST)
public String insertEmpDetails(@Valid @ModelAttribute("employee") Employee emp, BindingResult result, ModelMap model) {
if (result.hasErrors()){
model.addAttribute("error", "Your custom error messages");
return "<<back to form page without redirection>>";
}else {
empService.insertEmpDetails(emp);
return "redirect:/getList";
}
}
Read more about Spring - Validation, Data Binding, and Type Conversion
Read a post on Spring MVC : How to perform validation ?