原来在jsp页面上使用的是
${pageContext.request.contextPath}
使用SpringMVC的拦截器,自定义的拦截器实现HandlerInterceptor接口,用户所有的请求都会被拦截器拦截,可以在preHandle和和postHandle中编写自己的业务逻辑
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//如果不是转发的view
if (null != modelAndView) {
if (!(modelAndView.getView() instanceof RedirectView)) {
String basePath = httpServletRequest.getContextPath();
modelAndView.addObject("basePath",basePath);
}
}
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
在SpringMVC的配置文件中配置拦截器
<!--拦截器-->
<mvc:interceptors>
<bean class="com.xx.interceptors.LoginInterceptor"></bean>
</mvc:interceptors>
这样即可在页面中使用${basePath}来获取到项目路径
最后
以上就是阳光钻石最近收集整理的关于使用SpringMVC的拦截器interceptor解决项目路径问题的全部内容,更多相关使用SpringMVC内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复