Nav*_*ngh 2 ajax jquery spring-mvc
我正在处理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>
Run Code Online (Sandbox Code Playgroud)
控制器:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
更新{1}:
我尝试更改控制器,它开始给我错误:[object XMLHttpRequest]
@RequestMapping("/noticesAjaxRequest")
public @ResponseBody List<Notice> noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {
noticeBO.prepareNoticeList(noticeForm,model);
return noticeForm.getNoticeList();
}
Run Code Online (Sandbox Code Playgroud)
错误描述:
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
Run Code Online (Sandbox Code Playgroud)
请求标题
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
Run Code Online (Sandbox Code Playgroud)
谢谢各位朋友的帮助.最简单的方法是创建单独的jsp视图,并在从控制器返回/响应时包含该视图.
jQuery(document).ready(function(){
function doAjaxPost() {
// get the form values
var contextPath ='<jsp:expression>contextPath</jsp:expression>';
$.ajax({
type: "GET",
url: contextPath+"/noticesAjaxRequest",
success: function(data){
// we have the response
$('#info').html(data);
/* $('#info').refresh(); */
},
error: function(e){
alert('Error: ' + e);
}
});
}
setInterval(doAjaxPost,10*1000);
});
Run Code Online (Sandbox Code Playgroud)
控制器:
@RequestMapping(value="/noticesAjaxRequest")
public ModelAndView noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {
noticeBO.prepareNoticeList(noticeForm,model);
return new ModelAndView("/noticesList","noticeForm",noticeForm);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11553 次 |
| 最近记录: |