[英]How to call one controller method in another controller with RedirectAttributes
我寫的代碼如下。
Controller1 {
@Autowired
Controller2 controller2
//Caller method
void method1() {
controller2.furnction1(model,redirectattributes);
}
}
Controller2 {
public void function1(Model model, RedirectAttributes atr){
}
}
問題是:如何在我的新Controller( Controller1 )中初始化RedirectAttributes ( redirectattributes )。 我的Controller1不是表單提交,因此默認情況下我無法獲取RedirectAttributes 。
如何在controller2中調用funcation1。
重定向在這里不起作用。 所有表格數據將丟失。 嘗試將請求轉發到另一個URL。
嘗試類似
Controller1 {
@RequestMapping("url1")
public String method1() {
return "forward:/url2";
}
}
Controller2 {
@RequestMapping("/url2")
public String function1(Model model, RedirectAttributes atr){
//do something
}
}
我認為從另一個主計長調來一個主計長不是一個好習慣。 嘗試使用return redirect:/ function1在Controller2內部調用function1。 如果這是一種常見的功能/服務,請嘗試編寫一個可以幫助您完成工作的助手。
與POST請求相關的RedirectAttributes是什么?
只需將其作為參數添加到您的第一個控制器方法中即可。
void method1(RedirectAttributes redirectattributes) {
controller2.furnction1(model,redirectattributes);
}
但對方說,這不是一個好的模式,您應該使用類似以下內容的代碼:
return "redirect:/some/url";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.