BAEL-3821: Add new section in "Spring redirect and forward" article (#10502)

This commit is contained in:
freelansam 2021-02-20 22:27:17 +05:30 committed by GitHub
parent c29b355b02
commit 5832baeccc
2 changed files with 32 additions and 1 deletions

View File

@ -64,5 +64,11 @@ public class RedirectController {
public ModelAndView redirectedPostToPost() {
return new ModelAndView("redirection");
}
@RequestMapping(value="/forwardWithParams", method = RequestMethod.GET)
public ModelAndView forwardWithParams(HttpServletRequest request) {
request.setAttribute("param1", "one");
request.setAttribute("param2", "two");
return new ModelAndView("forward:/forwardedWithParams");
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.sampleapp.web.controller.redirect;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
@Controller
@RequestMapping("/")
public class RedirectParamController {
@RequestMapping(value = "/forwardedWithParams", method = RequestMethod.GET)
public RedirectView forwardedWithParams(final RedirectAttributes redirectAttributes, HttpServletRequest request) {
redirectAttributes.addAttribute("param1", request.getAttribute("param1"));
redirectAttributes.addAttribute("param2", request.getAttribute("param2"));
redirectAttributes.addAttribute("attribute", "forwardedWithParams");
return new RedirectView("redirectedUrl");
}
}