Java 将重定向发送到函数而不是url
我有以下功能:Java 将重定向发送到函数而不是url,java,Java,我有以下功能: @GetMapping("/auth/login") public String loginFunction(HttpServletRequest request, Model model) { return "login"; } 是否可以在另一个函数中调用该函数而不是url路径?目前我正在做: response.sendRedirect("/auth/login"); 但我更愿意这样做: response.sendRedirect(loginFunction) 因
@GetMapping("/auth/login")
public String loginFunction(HttpServletRequest request, Model model) {
return "login";
}
是否可以在另一个函数中调用该函数而不是url路径?目前我正在做:
response.sendRedirect("/auth/login");
但我更愿意这样做:
response.sendRedirect(loginFunction)
因为Spring MVC url映射方法只是方法 您可以从其他方法调用。不要重定向,打电话就行了
@GetMapping("/anotherMethod")
public String anotherMethod(HttpServletRequest request, Model model) {
// response.sendRedirect("/auth/login"); you can redirect
return loginFunction(request, model);
}
如果这不是您所期望的,请发布代码的调用部分。不,这是不可能的。好奇:为什么您要从
sendRedirect
中强制转换返回值?@Andreas是的,这是可能的。如果您正在使用的函数返回一个字符串。@Gatusko,它不会执行OP所要求的操作。OP希望发送一个重定向,该重定向将导致一个新的请求调用有问题的函数,而不是调用函数并让函数返回一个URL到。。。它本身OP并没有消除重定向。