I made an interceptor so that every time the system had a message to display I would trigger a javascript with the message.
public class MessagesInterceptor extends HandlerInterceptorAdapter {
public static final String urlBase = "http://localhost:8084";
public static String urlToRedirect = "";
public static String message = "";
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView)
throws Exception {
if(message.length() > 0){
PrintWriter pw = response.getWriter();
pw.write("<script>"
+ "window.alert('"+message+"'); "
+ "location.href='" + urlBase + urlToRedirect + "';"
+ "</script>");
pw.close();
message = "";
}
}
}
But my problem is what url
will redirect.
I created a urlToRedirect
variable to tell which url
to redirect.
I would like to not have it and find out if there is any way to get where the action
is redirecting without having to move to a variable inside the interceptor.
@RequestMapping(Routes.basicExercisesAct)
public String runExercise(HttpServletRequest request, Model model){
resolution = request.getParameter("resolution");
//javax.swing.JOptionPane.showMessageDialog(null, resolution);
exercise.buildGrading(resolution);
if (exercise.hasCompileErrors != true) {
//exercicio.salvarBancoDeDados(codigoUsuario, conexao);
if (chooser.canDoNextExercise() == true) {
MessagesInterceptor.urlToRedirect = Routes.basicExercisesNew;
return "redirect:"+Routes.basicExercisesNew;
} else {
//javax.swing.JOptionPane.showMessageDialog(null, "Parabéns. Você passou no teste!");
MessagesInterceptor.urlToRedirect = Routes.main;
return "redirect:"+Routes.main;
}
} else {
//exercicio.salvarBancoErroDeCompilacao(codigoUsuario, conexao);
if (exercise.endOfAttempts == true) {
if (chooser.canDoNextExercise() == true) {
/*javax.swing.JOptionPane.showMessageDialog(null, "Estouro de "
+ "quantidade de tentativas atingido. "
+ "Por favor, fazer o próximo exercício");
*/
MessagesInterceptor.urlToRedirect = Routes.basicExercisesNew;
return "redirect:"+Routes.basicExercisesNew;
} else {
//javax.swing.JOptionPane.showMessageDialog(null, "Você foi reprovado no teste");
MessagesInterceptor.urlToRedirect = Routes.main;
return "redirect:"+Routes.main;
}
}
else {
MessagesInterceptor.urlToRedirect = Routes.basicExercisesUpdate;
return "redirect:"+Routes.basicExercisesUpdate;
}
}
}
If the question is not clear, let me know if I can reform it.