redirect如何让url忽略model

本文翻译自http://stackoverflow.com/questions/2163517/how-do-i-prevent-spring-3-0-mvc-modelattribute-variables-from-appearing-in-url

 

三种方式:拦截器、viewresolver、redirect前model.asMap().clear()清除modelmap

 

1.拦截器中处理

 

     public class CommonModelMapInformationInterceptor implements
    HandlerInterceptor {

    // This method is unused as this Interceptor is for post handle only
    public void afterCompletion(HttpServletRequest request,
    HttpServletResponse response, Object handler, Exception ex)
    throws Exception { }

    // This method is unused as this Interceptor is for post handle only
    public boolean preHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler) throws Exception {
    return true;
    }

    public void postHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler,
    ModelAndView modelAndView) throws Exception {

    boolean isRedirectView = modelAndView.getView() instanceof RedirectView;
    boolean isViewObject = modelAndView.getView() == null;
    // if the view name is null then set a default value of true
    boolean viewNameStartsWithRedirect = (modelAndView.getViewName() == null ? true : modelAndView.getViewName().startsWith(UrlBasedViewResolver.REDIRECT_URL_PREFIX));

    if(modelAndView.hasView() && (
    ( isViewObject && !isRedirectView) ||
    (!isViewObject && !viewNameStartsWithRedirect))){
    addCommonModelData(modelAndView);
    }
    }

    public void addCommonModelData(ModelAndView modelAndView){
    modelAndView.addObject(“stuff”, “importantStuff”);
    modelAndView.addObject(“moreStuff”, “moreImportantStuff”);
    }
    }

 

 

2.viewresolver中处理

   我自己用的是继承自InternalResourceViewResolver,

   分别介绍两种方式:

   1)可以在需要忽略model的时候改用 redirectWithoutModel来做跳转

public class RedirectViewResolver implements ViewResolver, Ordered {
    // Have a highest priority by default
    private int order = Integer.MIN_VALUE; 

    // Uses this prefix to avoid interference with the default behaviour
    public static final String REDIRECT_URL_PREFIX = "redirectWithoutModel:";     

    public View resolveViewName(String viewName, Locale arg1) throws Exception {
        if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
            String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
            return new RedirectView(redirectUrl, true, true, false);
        }
        //return null;//此处原文返回空
        return super.resolveViewName(viewName, arg1);
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}

   2)当整个应用都不希望带model跳转,可以更简单的实现

  

public class RedirectsNotExposingModelUrlBasedViewResolver extends UrlBasedViewResolver {

    @Override
    protected View createView(String viewName, Locale locale) throws Exception {
        View view = super.createView(viewName, locale);
        if (view instanceof RedirectView) {
            ((RedirectView) view).setExposeModelAttributes(false);
        }
        return view;
    }

}

 

-----------------------------------------------------------------------------------------------

3.1以后还可以设置ignoreDefaultModelOnRedirect属性为true

你可能感兴趣的:(springMVC,redirect,url忽略model)