Last active
September 4, 2020 12:14
-
-
Save nguyennhatlinh1990/b6ecfc702c1bf0852f6b to your computer and use it in GitHub Desktop.
How to redirect in Spring MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.linhnguyen.demo.controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.ModelAttribute; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | |
import org.springframework.web.servlet.view.RedirectView; | |
public class RedirectController { | |
/** | |
* Redirect to internal url | |
*/ | |
@RequestMapping("/") | |
public String home(RedirectAttributes attributes) { | |
//Pass data to redirect page | |
attributes.addFlashAttribute("message", "This is message!"); | |
//Redirect to request mapping "target" | |
return "redirect:/target"; | |
} | |
@RequestMapping("/target") | |
public String redirectTarget(@ModelAttribute String message, Model model) { | |
//Pass data to redirect page | |
model.addAttribute("message", message); | |
return "targetPage"; | |
} | |
/** | |
* Redirect to external URL | |
*/ | |
@RequestMapping("/redirect") | |
public RedirectView redirectWithRedirectView(){ | |
RedirectView redirectView = new RedirectView(); | |
redirectView.setUrl("https://www.google.com.vn"); | |
return redirectView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment