Ajax 调用 servlet 并加载新页面

问题描述 投票:0回答:1

我想通过单击 id="test" 的 href 链接来加载 Ajax 加载器图像。单击此 href 后,我想调用 Jsp Servlet,它将发送一个新请求来加载新的 .jsp 页面。

此时的问题是 servlet 会被调用,但它不会对新页面进行请求。有人可以帮我吗?

我的代码如下所示:

jsp页面:href链接和loader 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>

这部分将由 servlet responseDispatcher 包含一个新的 jsp 页面:

<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>
jquery ajax jsp servlets
1个回答
0
投票

距我使用 JavaEE 已经过去很多年了,但据我记得,如果不连接输出,渲染 jsp 的标准方法是使用

RequestDispatcher.forward()
方法。所以也许这也是问题所在。

但我绝对可以看到的是,无论您的页面是什么,您都没有包含您的回复。将其添加到您的

success
处理程序中:

success: function(resp){
    $('#load').removeClass('loader');
    $('#load').html(resp);
},
© www.soinside.com 2019 - 2024. All rights reserved.