日期:2014-05-17  浏览次数:20890 次

struts2 页面重定向后找不到文件路径
我想做一个登录拦截,拦截器的代码如下
Java code

 public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
         HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        if(getCookieByName(request, "AdminLogin_cookie")!=null){
            request.getRequestDispatcher("/admiLogin/L.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("").forward(request,response);
        }
        
 }


web.xml配置
XML code


<filter>
   <filter-name>FilterAction</filter-name>
    <filter-class>
       FilterAction.FilterAction
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>FilterAction</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


现在是可以实现页面跳转了,但是跳转后的页面里引用的JS和CSS等文件全都找不到路径了,不加拦截单独访问的时候是没有问题的,求高人指点


------解决方案--------------------
那你JSP也没有basePath吗
探讨
引用:

JS和CSS的引用路径是什么

<link rel="stylesheet" href="css/common.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
这个2个

------解决方案--------------------
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<link href="<%=basePath%>css/css.css" rel="stylesheet" type="text/css" />