我想在单击 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>
最佳答案
距离我使用 JavaEE 已经过去很多年了,但据我所知,如果不连接输出,渲染 jsp 的标准方法是使用 RequestDispatcher.forward()
方法。所以也许这也是问题所在。
但我绝对可以看到的是,无论您的页面是什么,您都没有包含您的回复。将其添加到您的成功
处理程序中:
success: function(resp){
$('#load').removeClass('loader');
$('#load').html(resp);
},
关于jquery - Ajax 调用 servlet 并加载新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27998846/