我有一个jsp页面,其中它们是标题,左右面板,标题和左面板是静态的。左侧面板具有href,如果我们单击链接,则会在右侧面板中加载一个新页面。我想在不刷新整个页面的情况下使用Jquery Ajax实现此任务。
现在,我的查询是使用jquery ajax单击链接/按钮,我调用servlet,而我从servlet中我想设置jsp动态值,然后在右侧面板中加载更新后的值。
我尝试设置值,但是我得到的是空值。您能帮我在jsp中设置动态值吗?
下面是单击按钮时的示例代码,如果我成功执行此操作,则希望包含带有动态变量“ message”的hello.jsp,可以解决上述问题。
index.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JQuery AJAX Testing</title>
<script src="js/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function(){
$.ajax({
type : "post",
url : "AjaxServlet",
success : function(responseText) {
$('#message').load("jsp/hello.jsp");
},
error : function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
});
</script>
</head>
<body>
<div id="ajaxtest">
<h2>AJAX Testing using JQuery</h2>
</div>
<div id="message"></div>
<button>Change Content</button>
</body>
</html>
AjaxServlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Servlet post method is called");
// I want to set this value in hello.jsp
request.setAttribute("message", "I am from request get attribute");
response.getWriter().write("This is reply from servlet");
}
hello.jsp
<h1>Hello this page is include from index JSP</h1>
<%=request.getAttribute("message")%>
我想在从JQuery加载时在servlet的hello.jsp中设置消息
它说
您想要做的是,成功获取请求参数“ message”,而不是“ jsp / hello.jsp”,然后将请求参数返回模板,而不是整个jsp文件。
我不认为可以像这样加载一个jsp文件并正确解释,再加上load()将通过第二个ajax请求加载该文件,并且永远不会通过第一个请求将任何内容返回给模板。
尝试:
success : function(responseText) {
$('#message').html(responseText);
}
还要查看此堆栈上的Q / A,以了解Ajax返回值的原理:如何返回异步调用的响应?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句