在Ajax请求之后填写Bootstrap表(JSP和SpringMVC)



嗨,在将变量发送到Ajax函数后,我试图在JSP视图中填充表。

<script type="text/javascript">
function filterByDate() {
var count = 680;
$.ajax({
url : 'filterOnDate',
data : {
"count" : count
}, //here you send the daterange over an Ajax request and by default it's sended with a GET method
success : function(data) {
//alert(data); //here you will see an alert displaying the callback result coming from your spring controller
console.log("Request succeeded!");
console.log(data);
},
error : function(xhr, ajaxOptions, thrownError) {
if (xhr.status == 404) {
alert(thrownError);
}
}
});
}
</script>

这个Ajax请求被发送给UserController,UserController接收这个变量并将其发送给Model,以便在数据库中执行Hibernate搜索条件。

@RequestMapping(value = "/eblinb2b/filterOnDate", method = RequestMethod.GET)
public @ResponseBody List<EblInvB2B> filterByDate(Model model, @RequestParam("count") int count) {
// Fetch data from the DAO
List<EblInvB2B> eblinb2b_filter_counting = accountSservice.findByDateRangeEB2B(count);
// We add to the model (JSP page the list of EBLINVB2B)
model.addAttribute("eblinb2b_filter_counting", eblinb2b_filter_counting);
return eblinb2b_filter_counting;
}

我已经检查了hibernate查询是否在我放置调试断点后从COLUMN表中检索信息,我看到了带有对象的列表。

这是具有Hibernate标准的DAO方法:

@SuppressWarnings("unchecked")
@Override
public List<EblInvB2B> findDateRange(int count) {
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("count", count));
return (List<EblInvB2B>) criteria.list();
}

我想做的是用对我的Ajax的响应来填充我的表,只使用我应用标准的行,即来自Ajax请求的计数,它等于680,它是一个整数,应该只使用一行来填充jsp表。

仅供参考:我有一个DifferentJSP视图,其中有一个用于填充mySql数据库的更新按钮,它实际上是一个批处理,用于解组XML文件并将它们放入数据库。这是用户控制器方法:

@RequestMapping(value = "/eblinb2b/OutInCompare", method = RequestMethod.GET)
public String eblinb2bOutInCompare(Model model) {
// Fetch data from the DAO
List<EblInvB2B> eblinb2b_list = accountSservice.findAllEblInvB2B();
// We add to the model (JSP page the list of EBLINVB2B)
model.addAttribute("eblinb2b_list", eblinb2b_list);

return "eblinb2bCompare";
}

在这里,我想在我的视图中显示由Controller方法传递到JSP视图中的列表。我不知道它是否正确?:引导页面

https://jsfiddle.net/eaL38ejr/

感谢大家的帮助!

您应该在从ajax获取数据后再次加载引导表。

<script type="text/javascript">
function filterByDate() {
var count = 680;
$.ajax({
url : 'filterOnDate',
type: 'GET',
dataType:'json',
data : {
"count" : count
}, //here you send the daterange over an Ajax request and by default it's sended with a GET method
success : function(data) {
//alert(data); //here you will see an alert displaying the callback result coming from your spring controller
console.log("Request succeeded!");
console.log(data);
$('#tableID').bootstrapTable('load', data);
},
error : function(xhr, ajaxOptions, thrownError) {
if (xhr.status == 404) {
alert(thrownError);
}
}
});
}
</script>

或者刷新,

$('#tableID').bootstrapTable('refresh', {
url: 'filterOnDate?count='+count
});

编辑

Ajax 406表示您的请求不可接受,因此您需要更新控制器方法,如下所示。

@RequestMapping(value = "/eblinb2b/OutInCompare", method = RequestMethod.GET, 
headers="Accept=*/*",produces = "application/json")
public @ResponseBody List<EblInvB2B> filterByDate(Model model, @RequestParam("count") int count) {
// Fetch data from the DAO
List<EblInvB2B> eblinb2b_filter_counting = accountSservice.findByDateRangeEB2B(count);
// We add to the model (JSP page the list of EBLINVB2B)
model.addAttribute("eblinb2b_filter_counting", eblinb2b_filter_counting);
return eblinb2b_filter_counting;
}

以及在ajax请求中包含typedataType参数。如果有帮助,请告诉我。

最新更新