Interceptor in Spring MVC

2

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.

    
asked by anonymous 21.01.2015 / 00:53

1 answer

2

I do not think it's a good approach to use an interceptor to do redirect actions and display popups on the screen. In question the interceptor is usually an Application component scopped so using static attributes will generate access problems with concurrent users, where a user may receive a message that should be intended for the other user.

Regarding your script inside the interceptor, how about swapping it for a view with the code inside? We use a view only to do the redirect and display the messages of success:

redirect.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8"%>
<p style="margin-top: 10px">
    Redirecionando...
</p>
<script type="text/javascript">
    window.location = '${location}';
</script>

No controller only

return new ModelAndView("redirect", "sua-url-para-redirecionar");

In the end I did not answer your question but I hope I have helped.

    
25.02.2015 / 02:35