区分它是否是重定向:或HandlerInterceptorAdaptor中的正常请求

时间:2015-08-14 09:55:36

标签: java spring-mvc

如何区分重定向:或者不在HandlerInterceptorAdaptor.preHandle()中?我需要清除每个新请求的会话属性,除了它是一个重定向:来自控制器类。

e.g。

拦截器类

public class RequestInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        //CHECK whether it's a redirect:, if NO then clear session attribute.
        HttpSession session = req.getSession(false);
        session.removeAttribute("attributeKey");
    }
}

控制器类

@RequestMapping(value = "/detail", method = RequestMethod.GET)
private String viewDetails(HttpSession session, ...) {
    //program logic here
}

@RequestMapping(value = "/updatedetail", method = RequestMethod.GET)
private String updateDetails(HttpSession session, ...) {
    //program logic here
    //set session attribute
    session.setAttribute("attributeKey", someobject);

    //redirect, but i want the session attribute to be visible to the page instead of cleared by interceptor
    return "redirect:detail";
}

2 个答案:

答案 0 :(得分:0)

model.addAttribute("attributeKey", someObject);
return "redirect:detail";

隐藏attributeKey

<bean id="myBean" class="org.springframework.web.servlet.view.RedirectView">
    <property name="exposeModelAttributes" value="false"/>
    <property name="url" value="/detail"/>
</bean>

答案 1 :(得分:0)

您可以将此行更改为您的代码。 HttpSession session = req.getSession(true);