2015-01-17 79 views
2

我想通過點擊id =「test」的href鏈接加載Ajax加載器圖像。點擊這個href後,我想調用一個Jsp Servlet,它會發送一個新的請求來加載一個新的.jsp頁面。Ajax調用servlet並加載新頁面

現在的問題是該servlet將被調用,但他不會對新頁面發出請求。有人可以幫我嗎?

我的代碼如下所示:

jsp頁面:HREF鏈接和裝載機DIV

<div id="load"></div> 
<a href="#" id="test" title="#" class="mainLink">link</a> 

的Servlet:

public class TestController extends HttpServlet { 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
     // redirect to post 
     doPost(request,response); 
    } 

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
     // send to new jsp page 
     request.setAttribute(getServletContext().getInitParameter("DynamicContentLoader"),"/secure/pages/test/testPage.jsp"); 
     request.getRequestDispatcher("/collector.jsp").include(request, response); 
    } 
} 

Ajax調用:

<script type="text/javascript"> 
    $("#test").click(function(e){ 
     e.preventDefault(); 
     $.ajax({ 

     type: "POST", 
     url: "secure/TestController.do", 

     beforeSend : function(xhr, opts){ 
      //show loading gif 
      $('#load').addClass('loader'); 
     }, 
     success: function(){ 
      // nothing to do at this time 
     }, 
     complete : function() { 
      //remove loading gif 
      $('#laden').removeClass('loader'); 
     } 
    }); 
    }); 
</script> 

這是該部分將包括一個新的jsp頁面由servlet responseDispatcher:因爲我用的JavaEE但據我記得標準的方式來呈現JSP是使用RequestDispatcher.forward()方法,如果你不串聯輸出

<c:when test="${not empty requestScope.includeJspContent}"> 
    <!-- start Dynamic Generated Context --> 
    <jsp:include page="${requestScope.includeJspContent}" /> 
    <!-- end Dynamic Generated Context --> 
</c:when> 
<c:otherwise> 
    <!-- start no dynamic data found --> 
    <jsp:include page="/error/no_dynamic_content.jsp" /> 
    <!-- end no dynamic data found --> 
</c:otherwise> 

回答

0

太多年離開了。所以也許這也是問題所在。

但我可以肯定看到的是,您只是不包括您的迴應,無論它是什麼,你的網頁。將它添加到您的success處理程序中:

success: function(resp){ 
    $('#load').removeClass('loader'); 
    $('#load').html(resp); 
}, 
+0

我想使用裝載程序img加載新的jsp頁面。我將使用requestDispatcher設置此頁面的位置。 – CodeWhisperer

+0

我已經包含將顯示一個新的jsp頁面的部分 – CodeWhisperer