Aladdin
Aladdin

Reputation: 1337

Forwarding to another spring controller that receive parameter from POST action

I have a request mapping for a controller let's say it's A, it receives post action and uses its post values as parameter, sometimes the parameters will be very long so that's the reason why it's POST not GET apart from the best practices and security;

RequestMapping(value = "/reports/performance/indicator/{indicatorType}", method = RequestMethod.POST)
    public String generatePerformanceReportsIndicator(ModelMap map,HttpServletResponse response, @PathVariable("indicatorType") Long indicatorType,
                                                      @RequestParam(value = "siteIds", required = false) List<Long> siteIds,
                                                      @RequestParam(value = "timeframeIds", required = false) List<String> timeframeIds,
                                                      @RequestParam(value = "showTarget", required = false) String showTarget,Locale locale) { 

And then it turned out that in another spring controller I need to forward the request to the first one.

The problem is how I can add post parameters to the request before forwarding it to the first request mapping? is that healthy to say for example?

new FirstController().generatePerformanceReportsIndicator(....);

Given that:

Upvotes: 0

Views: 563

Answers (1)

ddarellis
ddarellis

Reputation: 3960

You should not manually call other controllers! What you can do is redirect to them with RedirectAttributes like:

@RequestMapping(value = "/doctor/doEditPatientDetails", method = RequestMethod.POST)
public String editPatientDetails(Model model, @ModelAttribute(value = "user") @Valid User user,
        BindingResult result, RedirectAttributes attr, Principal principal) {
    if (null != principal) {
        if (result.hasErrors()) {
            attr.addFlashAttribute("org.springframework.validation.BindingResult.user", result);
            attr.addFlashAttribute("user", user);
            attr.addAttribute("id", user.getId());
            return "redirect:/doctor/editPatient/{id}";
        }
    }
    ....
    return "redirect:/doctor/patients";
}

@RequestMapping(value = "/doctor/editPatient/{id}", method = RequestMethod.GET)
public String showEditPatient(Model model, @ModelAttribute("id") String id, Principal principal) {
    if (null != principal) {
        //here you can access the model and do what everything you want with the params.
        if (!model.containsAttribute("user")) {
            model.addAttribute("user", user);
        }
    ....
    }

    return "/doctor/editPatient";
}

Note that, to redirect to a link like "redirect:/doctor/editPatient/{id}" you have to add the id in RedirectAttributes. Also not that there are many ways you can achive the same functionality like HttpServletRequest

Upvotes: 2

Related Questions