jsp页面重定向后地址栏controller名重复而导致报404错误

今天做ssm项目时遇到了这种错误

 看看代码:

  无关代码省略。。。
 22 <body> 
 23     <div id="container">
 24         <iframe id="header" src="${pageContext.request.contextPath }/header.jsp" width="980" height="136" frameborder="0" scrolling="no"></iframe>
 25         <div id="main">
 26     <div class="cat">
 27             <%
 28             List<Category> pcategories = (List<Category>)session.getAttribute("pcategories");
 29             if(pcategories==null){
 30                 response.sendRedirect("categoryController/getParentCategoryId");
 31                 return; 
 32             }
 33             for(int i = 0 ; i < pcategories.size() ; i ++){
 34                 Category pcategory = pcategories.get(i);
 35             %>
 36             <ul>
 37                  <h1><%=pcategory.getName() %></h1>
 38                  <%
 39                  Set<Category> ccategories = pcategory.getcCategory();
 40                     for(Category child : ccategories){
 41                 %>
 42                      <li><a href='${pageContext.request.contextPath }/info.jsp'><%=child.getName() %></a></li>
 43                  <%
 44                     }
 45                 %> 
 46             </ul>
 47             <%        
 48                 }
 49             %>
 50             无关代码省略了。。。
 68           <%
 69               List<Product> products = (List<Product>)session.getAttribute("products");
 70               if(products==null){
 71                 response.sendRedirect("productController/getImageById");
 72                 return; 
 73             }
 74               for(int k = 0 ; k < products.size() ; k++){
 75                   Product product = products.get(k);
 76                   /* Iterator<ImageDTO> imagesIter = dto.getImagesDTO().iterator(); */
 77                   Set<Image> images = (Set<Image>) product.getImages();
 78                   for(Image image : images){
 79                   %> 
 80                    <dl>
 81                     <dt>
 82                     <img src="${pageContext.request.contextPath }/upload/<%=image.getUrl() %>" alt="alt" width="90" height="90" />
 83                     </dt>
 84                     <dd>
 85                     <a href="${pageContext.request.contextPath }/infoController/getProductInfo?productIdStr=<%=image.getProductId() %>"><%=product.getName() %></a>
 86                     </dd>
 87                 </dl> 
 88                   <%
 89                   }
 90               %> 
 91           <%
 92               }
 93           %> 
 94            
133 </body>
134 </html>

上面红色部分就是重定向的代码,跑上面代码就报404,百度了几个小时,没找出解决方案,后来自己仔细想一想,确定是重定向的路径错误。

于是一开始做了以下修改:

if(pcategories==null){
  response.sendRedirect("${pageContext.request.contextPath }/categoryController/getParentCategoryId");
  return;
}

无脑加上${pageContext.request.contextPath }(@_@原谅我的新手。。。)

发现还是报错。。。

又百度了一堆还是没解决,自己又再想想,思路没错,应该是写法出错,嗯,是这样!

果然,做了以下修改后就正确了

if(products==null){
  response.sendRedirect(request.getContextPath()+"/productController/getImageById");
  return;
}

学习没有一蹴而就,放下急躁,一步一步扎实前进
原文地址:https://www.cnblogs.com/jpxjx/p/12164413.html