浏览 5959 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-12-24
最后修改:2008-12-24
开发中很常见的一个用例,操作完成后返回操作页面。 用代码表示就是: response.sendRedirect(request.getHeader("referer")); spring MVC里有RedirectView,如果能够把referer带上就更方便了。好在spring很灵活,自己增加这个功能吧。 实现一个View接口: public class RefererRedirectView implements View{ protected static final Log logger = LogFactory.getLog(RefererRedirectView.class); /* (non-Javadoc) * @see org.springframework.web.servlet.View#getContentType() */ public String getContentType() { return null; } /* (non-Javadoc) * @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { String returnUrl = request.getHeader("referer"); if(returnUrl==null || returnUrl.trim().equals("")){ returnUrl = "/"; } logger.debug("referer url["+returnUrl+"]"); response.sendRedirect(returnUrl); } }
ViewResolver 这个接口的实例可以在spring容器里有多个,组成一个链式结构,第一个如果无法处理转交下一个,直到被处理或全部失败。 再实现一个ViewResolver接口: public class RefererViewResolver implements ViewResolver,Ordered { protected Log logger = LogFactory.getLog(getClass()); //以referer:起始的viewName,将被此ViewResolver处理 public static final String REFERER_PROFIX = "referer:"; /* (non-Javadoc) * @see org.springframework.web.servlet.ViewResolver#resolveViewName(java.lang.String, java.util.Locale) */ public View resolveViewName(String viewName, Locale locale) throws Exception { if (!viewName.startsWith(REFERER_PROFIX)) { return null; } logger.debug("this is referer view."); return new RefererRedirectView(); } /* (non-Javadoc) * @see org.springframework.core.Ordered#getOrder() */ public int getOrder() { return Integer.MAX_VALUE-1;//这个ViewResolver排在UrlBasedViewResolver前一个的位置 } } 最后,在配置文件中声明这个viewResolver: <bean class="com.........RefererViewResolver"/> 这样,凡是以referer:起始的viewName,都会被redirect到请求发起的页面。 附测试类: @Controller public class SomeController { @RequestMapping("/test_referer.do") public String referer(){ System.out.println("do some thing ,then return to referer."); return "referer:"; } @RequestMapping("/test_test1.do") public String test1(){ return "front/user/test1"; } @RequestMapping("/test_test2.do") public String test2(){ return "front/user/test2"; } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-12-25
这个...真的这么无聊,没有人感兴趣吗...
|
|
返回顶楼 | |
发表时间:2008-12-25
hehe,确实没这么用过呢~很欣赏springmvc
Grails就是基于springmvc的 |
|
返回顶楼 | |
发表时间:2008-12-26
steven_cheng 写道 这个...真的这么无聊,没有人感兴趣吗...
估计这里用springmvc的人不多 |
|
返回顶楼 | |
发表时间:2008-12-26
真不错,我用springmvc,不过还没想到你这里呢~呵呵
@RequestMapping("/test_test2.do") 是stripes框架吗? |
|
返回顶楼 | |
发表时间:2008-12-28
Ahha, actually there are many people work with SpringMVC, it's a very excellent framework, maybe they dont agree with you that this is a very common requirement. They are more prefered to see your extention to do with EXCEL or PDF and so on
|
|
返回顶楼 | |