gpt4 book ai didi

spring - spring3 Controller 中的重定向

转载 作者:行者123 更新时间:2023-12-02 07:50:46 25 4
gpt4 key购买 nike

在 Spring 3 中,您可以像这样简单地映射 url:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return "index";
}

是否可以使这种方法重定向到另一个网址,例如:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return "second.html";
}

@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model) {
//put some staff in model
return "second";
}

最佳答案

您不需要重定向 - 只需调用该方法即可:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return second(model);
}

@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model) {
//put some staff in model
return "second";
}

这是注释风格的好处之一;您可以将您的方法链接在一起。

如果您确实想要重定向,那么您可以将其作为 View 返回:

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public View index(Model model) {
return new RedirectView("second.html");
}

@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model) {
//put some staff in model
return "second";
}

关于spring - spring3 Controller 中的重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5077783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com