您還可以跟蹤最後通過攔截器呈現給用戶的ModelAndView(或只是視圖)。此示例只跟蹤最後一個模型和視圖,但您可以使用列表導航回更多級別。
package com.sample;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LastModelAndViewInterceptor extends HandlerInterceptorAdapter {
public static final String LAST_MODEL_VIEW_ATTRIBUTE = LastModelAndViewInterceptor.class.getName() + ".lastModelAndView";
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
request.getSession(true).setAttribute(LAST_MODEL_VIEW_ATTRIBUTE, modelAndView);
super.postHandle(request, response, handler, modelAndView);
}
}
使用Spring XML配置:
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<bean class="com.sample.LastModelAndViewInterceptor"/>
</property>
</bean>
然後用下面的代碼回到該視圖控制器中的:
ModelAndView mv = (ModelAndView)request.getSession().getAttribute(LastModelAndViewInterceptor.LAST_MODEL_VIEW_ATTRIBUTE);
return mv;
絕對酷但可能不可靠:http://en.wikipedia.org/wiki/HTTP_referrer#Referrer_hiding – wheleph 2011-10-06 18:32:33
如果在編輯小馬時出現驗證錯誤,並且用戶返回到編輯小馬頁,這將不起作用。因爲現在引用者將指向編輯小馬頁面。 – priomsrb 2013-03-11 22:13:23