java - 发送重定向到函数而不是 url
问题描述
我有以下功能:
@GetMapping("/auth/login")
public String loginFunction(HttpServletRequest request, Model model) {
return "login";
}
是否可以在另一个函数中调用该函数而不是 url 路径?目前我正在做:
response.sendRedirect("/auth/login");
但更愿意这样做:
response.sendRedirect(loginFunction)
解决方案
因为 Spring MVC url-mapped 方法只是方法。
您可以从其他方法调用。不要重定向,只是打电话。
@GetMapping("/anotherMethod")
public String anotherMethod(HttpServletRequest request, Model model) {
// response.sendRedirect("/auth/login"); you can redirect
return loginFunction(request, model);
}
如果这不是您所期望的,请发布代码的调用部分。