1

I have a view that is rendered with this method:

@RequestMapping("/employee/{id}")
public String showSpecificEmployee(@PathVariable String id, Model model){

    model.addAttribute("employee", employeeService.findEmployeeById(new Long(id)));

    DateCommand dateCommand = new DateCommand();
    dateCommand.setEmployeeId(new Long(id));

    model.addAttribute("date", dateCommand);

    return "specificEmployee";
}

The view displayes some basic information about the Employee. On the same view, I do have a form to choose a month and filter the information by Date. After the Date is chosen, I would like to have the view 'refreshed' with updated information. That means I do have a POST & GET methods bound to the same view.

@RequestMapping("/passdate")
public String updateWorkmonth(@ModelAttribute DateCommand dateCommand, Model model){

    model.addAttribute("employee", employeeService.findEmployeeWithFilteredWorkdaysAndPayments(dateCommand.getEmployeeId(), dateCommand.getActualDate()));
    model.addAttribute("date", dateCommand);

    return "specificEmployee";
}

After the second method is invoked looks like http://localhost:8080/passdate?employeeId=1&actualDate=2018-02, but I want it to be /employee/{id}. How do I combine those 2 methods, so they point to the same URL?

If I set @RequestMapping("/employee/{id}") on both methods, I keep getting an error.

Maciaz
  • 1,084
  • 1
  • 16
  • 31
  • refer: https://stackoverflow.com/questions/17987380/combine-get-and-post-request-methods-in-spring – Gaurav Feb 14 '18 at 14:49

3 Answers3

1

You actually need only one GET method

@RequestMapping("/employee/{id}")

and optionally passed

@RequestParam("actualDate")
zlakad
  • 1,314
  • 1
  • 9
  • 16
Vadym Dudnyk
  • 162
  • 3
0

You can specify the type of HTTP request you want in the @RequestMapping parametters

When you don't specify it, it uses GET by default

@RequestMapping(value = "/employee/{id}",method = RequestMethod.POST)

zlakad
  • 1,314
  • 1
  • 9
  • 16
0

You can redirect user to that url. Just replace in method updateWorkmonth one line

return "specificEmployee";

with

return "redirect:/employee/" + dateCommand.getEmployeeId();
Anton Tupy
  • 951
  • 5
  • 16
  • This thing is it will not filter Employee information as it will invoke method bound to this URL. And that is initial data. – Maciaz Feb 14 '18 at 15:16