2015-01-17 165 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