首页 新闻 会员 周边

SpringMVC redirect相关问题

0
悬赏园豆:100 [待解决问题]

个删除页面,删除完成后用SpringMVC redirect到列表页面,代码如下:

@RequestMapping(value ="/{id}/delete",method = RequestMethod.GET)
    public String delete(@PathVariable String id,HttpServletRequest request) {
        userService.deleteById(id);
        return "redirect:/user/list";
    }

配置有一个全局的拦截器

    public void postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) throws Exception {
        if (null == modelAndView) {
            return;
        }
        // 系统配置参数
        String basePath = HttpUtils.getBasePath(request);
        String contextPath = HttpUtils.getContextPath(request);
        modelAndView.addObject("basePath", basePath);
        modelAndView.addObject("contextPath", contextPath);
    }

现在的问题是,redirect后的地址栏总是将modelAndView中的参数拼接上了,如下:

http://localhost:8080/macaque/user/list?basePath=http%3A%2F%2Flocalhost%3A8080%2Fmacaque&ssss=%2Fmacaque&contextPath=%2Fmacaque

但是如果用response.sendRedirect("")的话 地址栏正常如下: 

http://localhost:8080/macaque/user/list

求助 这是什么原因呢,怎么解决

Macaque的主页 Macaque | 初学一级 | 园豆:56
提问于:2014-05-16 09:34
< >
分享
所有回答(3)
0

"redirect:/user/list"; springmvc要自己生成一个modelAndView对象

后置拦截器这里if (null == modelAndView) { return; }就不满足

如果直接用servlet的api:response.sendRedirect("")if (null == modelAndView) { return; }这里的modelAndView就为null了

我为程序员自豪 | 园豆:202 (菜鸟二级) | 2014-05-19 11:37

恩,多谢,可springmvc为什么总会把modelAndView中的参数给拼到地址栏呢……。不想这样啊

支持(0) 反对(0) Macaque | 园豆:56 (初学一级) | 2014-05-19 11:44

@Macaque: 是redirect关键字引起的,重定向后的参数肯定要拼接到url后面,如果请求转发的话估计不会,你可以试试看

支持(0) 反对(0) 我为程序员自豪 | 园豆:202 (菜鸟二级) | 2014-05-19 18:20
0

我也遇见和你同样的问题,最近刚有一个比较合适的方法解决:

去我的博客看看:http://my.oschina.net/u/138589/blog/317210

月下美人 | 园豆:202 (菜鸟二级) | 2014-09-23 08:51
0

springMVC的配置文件注解开关中配置"ignoreDefaultModelOnRedirect"变量为true, 如下:
<mvc:annotation-driven ignoreDefaultModelOnRedirect="true"/>

大风少 | 园豆:202 (菜鸟二级) | 2016-12-15 11:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册