I would like to invoke a Controller's method via a Java class, so I can return a specific view. In this case I have a short list of IDs; if the current user's ID is not in that list, redirect to a view invalidUser.
I can do it on the client-side with either Ajax or a button: onclick="location.href='/invalidUser'
But I am not clear on how I can call the ViewsController's invalidUser() method from a Java class.
How can I do that with Java in the invalidUserRedirect() method? I was thinking to get the base URL from the HttpServletRequest as shown here: Get Root/Base Url In Spring MVC and then make an http call to baseUrl + "/invalidUser" but that does not seem like the right approach.
AuthService:
@Service
public class AuthService {
public void invalidUserRedirect(HttpServletRequest request) {
// Make call to invalidUser() in ViewsController
}
}
Views Controller:
@Controller
public class ViewsController {
@RequestMapping(value = "/invalidUser", method = {RequestMethod.GET})
public String invalidUser() {
return "invalid";
}
}