Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to another controller with a @PathVariable

How do I redirect to another controller with a path variable in the redirect. I tried it the following way but get this error:

java.lang.IllegalArgumentException: Model has no value for key 'formId'

How I implemented it:

Long formId = drugType.getFormId();
            view = "redirect:/pub/req/customForm/view/{formId}";

And received by the controller:

@RequestMapping(method = RequestMethod.POST,  value = "/pub/req/customForm/view/{formId}")
String completeCustomForm(@PathVariable Long formId,
        @Valid @ModelAttribute CustomFormLayout customFormLayout,
        BindingResult errors, HttpServletRequest request, Model model,
        RedirectAttributes attr) {

Any ideas how I can redirect to this controller with the formId value?

like image 270
Blawless Avatar asked Oct 18 '25 11:10

Blawless


2 Answers

You could either build the redirect address string:

return "redirect:/pub/req/customForm/view/" + drugType.getFormId();

Or add a model attribute named as your path variable ("formId") and use it in your view name (this is what the error message is telling you)

model.addAttribute("formId", drugType.getFormId());
return "redirect:/pub/req/customForm/view/{formId}";
like image 89
alfcope Avatar answered Oct 20 '25 01:10

alfcope


Try applying the parameter:

Long formId = drugType.getFormId();
view = "redirect:/pub/req/customForm/view/"+formId;
like image 28
MartinByers Avatar answered Oct 20 '25 00:10

MartinByers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!