0
@RequestMapping("/insert")  
         public String insertEmpDetails(@ModelAttribute("employee") Employee emp) {
          if (emp != null) 
          empService.insertEmpDetails(emp);  
          return "redirect:/getList";
         } 

Please tell me what to write in else part.I was trying for that but i am not getting

1 Answers1

0

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 ?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76