我正在处理 ajax 请求/响应以更新表格,
但是当 Ajax 调用控制器时,我得到了整个页面的响应。但我想要的只是精确的表数据,我需要用我的 c:forEach 表进行映射。谢谢。
jsp视图:
<script type="text/javascript">
jQuery(document).ready(function(){
function doAjaxPost() {
// get the form values
var contextPath ='<jsp:expression>contextPath</jsp:expression>';
$.ajax({
type: "GET",
url: contextPath+"/noticesAjaxRequest",
dataType: "json",
contentType: 'application/json',
success: function(data){
// we have the response
$('#info').empty().html(data);
},
});
}
setInterval(doAjaxPost,10*1000);
});
</script>
<div id="info">
<c:forEach items="${noticeForm.noticeList}" var="notice">
<c:out value="${notice.coreValue} "/>
<c:out value="${notice.description} "/>
<br/>
</c:forEach>
</div>
控制器:
@Controller
public class DashboardController {
private NoticeBO noticeBO;
/*@RequestMapping("/dashboardTest")
public String printWelcome(ModelMap model) {
List<Employee> employeeList=dashboardDAO.getAllEmployee();
for(Employee employee:employeeList)
model.addAttribute("msg", model.get("msg")+"<br/> Spring 3 MVC Hello World"+employee.getCustomerId());
return "DashboardTest";
}*/
public NoticeBO getNoticeBO() {
return noticeBO;
}
public void setNoticeBO(NoticeBO noticeBO) {
this.noticeBO = noticeBO;
}
@RequestMapping("/dashboard")
public String dashboard(ModelMap model) {
return "Dashboard";
}
@RequestMapping("/notices")
public ModelAndView notices(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {
noticeBO.prepareNoticeList(noticeForm,model);
return new ModelAndView("notices","noticeForm",noticeForm);
}
@RequestMapping("/noticesAjaxRequest")
public ModelAndView noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {
noticeBO.prepareNoticeList(noticeForm,model);
return new ModelAndView("notices", "noticeForm", noticeForm);
}
}
更新{1}:
我尝试更改控制器并开始给我错误:[object XMLHttpRequest]
@RequestMapping("/noticesAjaxRequest")
public @ResponseBody List<Notice> noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {
noticeBO.prepareNoticeList(noticeForm,model);
return noticeForm.getNoticeList();
}
错误描述:
HTTP 状态 406 - 此请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。
响应标头
Content-Length 1067
Content-Type text/html;charset=utf-8
Date Thu, 11 Jul 2013 12:48:19 GMT
Server Apache-Coyote/1.1
请求标头
Accept application/json, text/javascript, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cookie JSESSIONID=D54D66F6B7FE05C2B6FB684BF19387F1
Host localhost:8080
Referer http://localhost:8080/vServFinance/notices
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
X-Requested-With XMLHttpRequest