我的控制器:
@RequestMapping(value = "jobs")
public void removeJobs(@RequestParam("username") String username, Model model) {
String []jobs = jobsFetcher(username);
model.addAttribute("list", jobs);
}
我的 JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet"
href="<c:url value="public/files/styles/jobManager.css" />" />
<script type="text/javascript">
function ajaxFunction(username) {
var xmlhttpReq = crossBrowserAjaxObject();
if (xmlhttpReq) {
xmlhttpReq.open("get", "jobs", true);
xmlhttpReq.onreadystatechange = handleServerResponse(xmlhttpReq);
xmlhttpReq.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
xmlhttpReq.send("username=" + username);
}
}
function crossBrowserAjaxObject() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function handleServerResponse(xmlhttpReq) {
//var xmlhttp = new XMLHttpRequest();
return function() {
alert(xmlhttpReq.readyState);
if (xmlhttpReq.readyState == 4 && xmlhttpReq.status == 200) {
alert(xmlhttpReq.responseText); // this is ok
// HERE HOW DO I GET THE MODEL OBJECT AS A RESPONSE COMMING FROM SERVLET &
// USE IN MY JSP FILE ?
}
};
}
</script>
</head>
<body>
<div>
<div class="leftDiv">
<a href="#" onclick="ajaxFunction('JAMES')">JAMES</a>
<a href="#" onclick="ajaxFunction('David')">David</a>
<a href="#" onclick="ajaxFunction('Catrina')">Catrina</a>
<a href="#" onclick="ajaxFunction('Cathy')">Cathy</a>
<a href="#" onclick="ajaxFunction('Paul')">Paul</a>
</div>
<div class="rightDiv">
<!-- HOW DO I GET THE JOB LIST & USE IT HERE USING MY AJAX ? -->
<c:forEach items="???" var="task">
<p>${task}</p>
</c:forEach>
</div>
</div>
</body>
</html>
现在正如我在图片中提到的那样,我希望在单击员工而不刷新整个页面时更改右侧的 div 元素。
我唯一不知道的是当 xmlhttpReq.responseText 返回给我时,我将如何使用 Ajax 获取带有作业数组的模型元素并使用它来呈现页面......
换句话说,我如何能够在 Ajax 调用后从我的控制器获取参数并使用 Ajax 本身来制作我的页面?
你有什么想法或建议或什么可以帮助我完成这个吗?
(顺便说一下这些代码还没有测试!)