在使用springboot开发官网时,遇到这个问题。
解决思路:利用ajax,加载后台方法,写入到前端。
我是更具 nid 来获取上一条和下一条
sql部分:
上一篇 SELECT * FROM new_info WHERE nid<9 ORDER BY nid DESC LIMIT 1;
下一篇 SELECT * FROM new_info WHERE nid>9 ORDER BY nid ASC LIMIT 1;
serviceimp部分:
@Override
public NewsDO getpre(Map<String,Object> map) {
NewsDO newsDO=infoDao.getpre(map);
return newsDO;
}
@Override
public NewsDO getnext(Map<String,Object> map) {
NewsDO newsDO=infoDao.getnext(map);
return newsDO;
}
controller部分:
//获取上一页
@ResponseBody
@GetMapping("/getpre")
NewsDO getpre(@RequestParam Map<String, Object> params){
NewsDO info=infoService.getpre(params);
return info;
}
//获取下一页
@ResponseBody
@GetMapping("/getnext")
NewsDO getnext(@RequestParam Map<String, Object> params){
NewsDO info=infoService.getnext(params);
return info;
}
前端ajax部分:
function getpre() {
var nid = /*[[${info.nid}]]*/ //这里用到thymelea获取model中的值
var type= /*[[${info.type}]]*/
var status= /*[[${info.status}]]*/
$.ajax({
url: '/news/getpre?nid='+nid+'&type='+type+'&status='+status,
method: "get",
dataType : 'json',
success: function (data) {
var htmlText = "";
if (data.nid != null){
htmlText += '上一篇: <a href="/news/open/'+data.nid+'">';
htmlText += data.title+'</a>';
}
$("#info-pre").append(htmlText);
}
});
}
function getnext() {
var nid = /*[[${info.nid}]]*/
var type= /*[[${info.type}]]*/
var status= /*[[${info.status}]]*/
$.ajax({
url: '/news/getnext?nid='+nid+'&type='+type+'&status='+status,
method : 'get',
// dataType : 'json',
success : function(data) {
var htmlText = "";
if (data.nid != null){
htmlText += '下一篇: <a href="/news/open/'+data.nid+'">';
htmlText += data.title+'</a>';
}
$("#info-next").append(htmlText);
}
});
}