Passing a message into an action in Spinrg MVC controller from an interceptor

95 Views Asked by At

One of a controller's action

@RequestMapping("/user/login")
public String loginPage(String errorMessage, Model model) {
    model.addAttribute("errorMessage", errorMessage);

    return "/view/loginPage";
}

A snippet of viewPage.jsp

<div id="alertError">Failed to get data because : ${errorMessage}</div>

A snipper of my interceptor

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    HttpSession session = request.getSession();
    User user = (User)session.getAttribute("user")

    if ( user == null ) {
        response.sendRedirect("/user/login.do");
    }
}

Okay time to explain.

The interceptor checks a session and redirects to the signin page if session is null. I want to display a message your session was expired. Please sign in after the complete redirection.

But in the interceptor, I can't find any way to pass the message to the loginPage action. How can I do that?

FYI, Directly calling the loginPage method is not an answer for me.

1

There are 1 best solutions below

2
On

You can pass your message as query parameter.

response.sendRedirect("/user/login.do?errMessage=yourMessage");

And in the controller side it can be retrieved as,

@RequestMapping("/user/login")
public String loginPage(@RequestParam("errMessage") String errMessage, Model model) {