簡體   English   中英

重定向的網址未顯示在瀏覽器中

[英]Redirected url not showing in browser

我正在從另一個控制器(分別說controller1和controller2)中調用一個控制器的視圖。 它已成功運行,但是即使我重定向到controller2,瀏覽器也會顯示controller1的URL。 如何改變呢?

@Controller

@SessionAttributes

public class UserFormController {

@Autowired
private UserService userService;

@Autowired
private Controller2 controller2;

@RequestMapping(value = "/method1", method = RequestMethod.GET)
public ModelAndView redirectFormPage() {

 return controller2.redirectMethod();

}

此處顯示的是網址“ method1”。 我想顯示被調用的URL。

controller2.redirectMethod()有什么作用?

代替直接從控制器調用方法,使用此方法並將URL放入redirectMethod(redirectURL)

   return new ModelAndView("redirect:/redirectURL");

要么

   return "redirect:/redirectURL"

取決於你返回什么

在您的情況下,它將被視為常規方法。

控制器1:

@Controller
@RequestMapping("/")
public class Controller11 {     
    @RequestMapping("/method1")
    public String method1(Model model) {
        return "redirect:/method2";
        // If method return ModelAndView
        // return new ModelAndView("redirect:/method2");
    }
}

控制器2:

@Controller
public class Controller22 {
    @RequestMapping("/method2")
    public String method1(Model model) {
        model.addAttribute("method", "method2");
        return "method";
        //If method return ModelAndView
        //  model.addAttribute("method", "method2");        
        //  return new ModelAndView("method");
    }
}

視圖:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Method1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Method, ' + ${method} + '!'" />
</body>
</html>

Controller2編寫另一個處理程序,該處理程序將調用redirectMethod()

Controller2

@RequestMapping(value = "/redirectFromUser", method = RequestMethod.GET)
public ModelAndView handleRedirectionFromUser() {
    return redirectMethod();
}

UserFormController

@RequestMapping(value = "/method1", method = RequestMethod.GET)
public String redirectFormPage() {
    return "redirect:/url/to/redirectFromUser";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM