Interceptor at Spring MVC

Asked

Viewed 364 times

2

I made an Interceptor so that every time the system had a message to display 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 which url I’ll redirect. I created a variable urlToRedirect to tell which url I’ll redirect. I wish I didn’t have her and find out if there’s any way to get to where action is redirecting without having to pass 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.

1 answer

2

I believe that it is not a good approach to use the Interceptor to do redirect actions and display popups on the screen. In question to the Interceptor it is usually a component Application scopped so using static attributes will generate access problems with simultaneous users, where a user can receive a message that should be intended for another user.

In relation to your script inside the Interceptor how about exchanging it for a view with the code inside ? We use a view only to redirect and display successful messages:

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>

In controller only

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

In the end I didn’t answer your question but I hope I helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.