springboot项目练习四 与solr交互

版权声明: https://blog.csdn.net/Master_chaoAndQi/article/details/85941730

1 导入ligerUi的js插件

2 修改后台代码完成分页查询,增加根据id删除的方法

3 编写JQ完成页面的简单的交互

3 新增一个resultData类完成返回数据的封装

package com.gc.utils;

import java.io.Serializable;
/**
 * 返回结果数据
 * @author gc
 *
 */
public class ResultData implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String code; //状态码
	private Object data;//数据
	private String msg;//异常信息
	
	/**
	 * 构造方法
	 * @return
	 */
	
	public ResultData(){}
	public ResultData(String code,Object data,String message){
		this.code=code;
		this.data=data;
		this.msg =message;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
}

4 修改接口方法NewController.java中list的方法

/**
	 * list 返回所有的新闻数据
	 * @return
	 */
	@RequestMapping(value ="list" )
	@ResponseBody
	public PageBean<?> listNewData(String pagesize,String  currentPage){
		Map<String,Object> param = new HashMap<String,Object>();
		PageBean<NewDoc> page = new PageBean<NewDoc>(Integer.valueOf(currentPage),Integer.valueOf(pagesize));
		try {
			param.put("query", "source_news:*");
			param.put("pagesize", pagesize);
			param.put("currentPage", currentPage);
			page = newDocSolr.getByPage(param);
//			list = newDocSolr.getList("source_news:*",true);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return page;
	}
/**
	 * 根据id删除
	 * @return
	 */
	@RequestMapping("del/{id}")
	@ResponseBody
	public ResultData delById(@PathVariable("id") String id){
		try {
			newDocSolr.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new  ResultData("-1","",e.getMessage());
		}
		return new  ResultData("200","","删除成功!");
	}

5 修改CommonRepository.java增加分页查询的方法

PageBean<T> getByPage(Map<String,Object> param) throws Exception;

6 修改CommonRepositoryImpl实现类增加方法实现

/**
	 * 分页查询 
	 * param封装请求参数
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PageBean<T> getByPage(Map<String, Object> param) throws Exception {
		SolrQuery query = new SolrQuery();
		PageBean<T> page = new  PageBean<T>();
		List<T> list = new ArrayList<T>();
		if(param.containsKey("query")){
			query.setQuery(param.get("query").toString());
		}
		
		for (String  key:param.keySet()) {
			if(StringUtils.equals("currentPage", key)){
		query.setStart(Integer.valueOf(param.get(key).toString())*(Integer.valueOf(param.get(key).toString())-1));
				page.setCurrentPage(Integer.valueOf(param.get(key).toString()));
			}else if(StringUtils.equals("pagesize", key)){
				query.setRows(Integer.valueOf(param.get(key).toString()));
				page.setPageSize(Integer.valueOf(param.get(key).toString()));
			}
		}
		QueryResponse response = solrTemp.getSolrClient().query(collection, query, METHOD.GET);
		int status = response.getStatus();
		if (status == 0) {
				SolrDocumentList results = response.getResults();
				for (SolrDocument solrDocument : results) {
					list.add((T) solrDocument);
				}
				long numFound = results.getNumFound();
				page.setRows(list);
				page.setTotal(Integer.parseUnsignedInt(new String(numFound+"")));
			}
		return page;
	}

7 新增Pagebean.java文件封装分页结果

package com.gc.utils;

import java.io.Serializable;
import java.util.List;



public class PageBean<T> implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 当前页
	private Integer currentPage = 1;
	// 每页显示的总条数
	private Integer pageSize = 10;
	// 总条数
	private Integer total;

	// 总页数
	private Integer totalPage;
	// 开始索引
	private Integer startIndex;
	// 分页结果
	private List<T> rows;


	public PageBean() {
		super();
	}

	public PageBean(Integer currentPage, Integer pageSize) {
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}

	public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.total = totalNum;
		this.totalPage = (this.total + this.pageSize - 1) / this.pageSize;
		this.startIndex = (this.currentPage - 1) * this.pageSize;
		
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}
	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}
}

8 新增news.html 引入ligerui.all.js jquery-1.8.3.min.js文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
 <link href="../ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3 align="center">springboot-solr-新闻列表</h3>
<div id="tableContainer" class="l-layout-center" style="margin-top:10px;"></div>

<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src=../ligerUI/js/ligerui.all.js></script>
</body>
<script>
var $list_dataGrid ;
$(function(){
	var $list_dataGrid = $("#tableContainer").ligerGrid({
		columns:[
			{display: '操作', name: 'operate', align: 'center', width: 80,isSort:false,render:opterate},
			
			{display: '最后修改时间', name: 'lmodify', align: 'center', width: 100,isSort:false},
			{display: '来源', name: 'source_news', align: 'center', width: 70,isSort:false},
			{display: 'pc详情页面', name: 'url_3w', align: 'center', width: 200,isSort:false},
			{display: '手机详情页面', name: 'mtime', align: 'center', width: 200,isSort:false},
			{display: '标题', name: 'title_news', align: 'center', width: 200,isSort:false},
			{display: '评论数', name: 'replyCount_news', align: 'center', width: 60,isSort:false},
			{display: '描述', name: 'digest', align: 'left', width: 200,isSort:false},
			{display: '图片', name: 'imgsrc', align: 'center', width: 80,isSort:false,render:function(rowData){
				if(rowData.imgsrc[0]){
					return "<img style='width:50px;heigth:auto' src="+rowData.imgsrc[0]+"  />";
				}
			}},
			{display: '点击数', name: 'votecount', align: 'center', width: 190,isSort:false}
			
		],
		title:"springboot-新闻列表",
		usePager:true,
		pageSizeOptions:[20,50,100],
		pageSize:20,
		height:'99%',
		url:'/news/list',
		showTableToggleBtn :true,  
		allowAdjustColWidth:true, 
		checkbox :false,  
		cssClass :'',  
		root :'rows',  //对应pageBean中的rows
		record:'total',  //对应pageBean中的total
		delayLoad :false,  
		allowUnSelectRow:true,  
		allowUnSelectRow:true,  
		mouseoverRowCssClass :'l-grid-row-over',  
		rownumbers :true,  
		frozen:false, 
		frozenRownumbers :true,
	    enabledSort:true
		});
	
});
function opterate(rowData){
	return  "<a href='javascript:void(0)' onclick='del("+rowData.id+")'>删除</a>"
}
function del(id){
	var url ="/news/del/"+id;
	$.ligerDialog.confirm( '确定删除该行数据?', function(){
		$.post(url,{},function(res){
			if(res.code=='200'){
				$.ligerDialog.tip({icon: 'succeed', time: 1, content:res.msg});
				setTimeout(function(){
					$(".l-bar-button.l-bar-btnload",window.parent.document).click();
				},1000);
			}else{
				$.ligerDialog.tip(res.msg);
 			}
 		},'json');
 		return true;
 	},function(){
		return true;
	});
}
</script>

</html>

9 界面截图

界面效果已经完成了,现在还有好多地方可以进行优化,在接下来的时候会对返回的结果的实体类,查询方法进行优化。

下一步:在点击页面上的超链接的时候,将链接传到后台通过流的方式生成本地的Html文件完成请求的静态化。项目下载地址:

链接:https://pan.baidu.com/s/1JYAsjjMCPQ4nQJFMVCLm5w 
提取码:i8sz 

猜你喜欢

转载自blog.csdn.net/Master_chaoAndQi/article/details/85941730