Solo  当前访客:4 开始使用

JAVA日知录

一个关注| Java | Spring Boot | Spring Cloud | 干货分享的博客网站

AOP 更新 ElasticSearch 索引---前端数据展示

2020-04-23 14:29:44 shuikan95
0  评论    121  浏览

前端数据展示

  • 路由文件 IndexController
package com.lab.es.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping({"/", "index"})
    public String index() {
        return "index";
    }
}
  • 数据展示 ItemController
package com.lab.es.demo.controller;

import com.lab.es.demo.common.Res;
import com.lab.es.demo.common.ResFactory;
import com.lab.es.demo.common.ResPaging;
import com.lab.es.demo.dto.SearchItemDTO;
import com.lab.es.demo.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;

    @PostMapping("/search")
    public Res search(@RequestBody(required = true) SearchItemDTO searchItemDTO) throws IOException {
        ResPaging resPaging = itemService.searchPageHighlight(searchItemDTO);
        Res res = ResFactory.getInstance().getEmptyRes();
        res.setData(resPaging);
        res.setCode(100);
        res.setMessage("ok");
        return res;
    }

}
  • 搜索 DTO SearchItemDTO
package com.lab.es.demo.dto;

import lombok.Data;

@Data
public class SearchItemDTO {
    private Integer pageNo;
    private Integer pageSize;
    private String keyword;
    private String sort;
}
  • 通用类 Res
package com.lab.es.demo.common;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@lombok.Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class Res implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private Object data = "";
    private String message = "";
}
  • 通用类 ResPaging
package com.lab.es.demo.common;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

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

@Data
public class ResPaging implements Serializable {
	private static final long serialVersionUID = 1L;

	@JSONField(ordinal = 1)
	private List<?> records;

	@JSONField(ordinal = 2)
	private Integer pageSize;

	@JSONField(ordinal = 3)
	private Integer pageNo;

	@JSONField(ordinal = 4)
	private Integer total;

	@JSONField(ordinal = 5)
	private Integer pageCount;

	@JSONField(ordinal = 6)
	private String prevPage;

	@JSONField(ordinal = 7)
	private String nextPage;
}
  • 通用类 ResFactory
package com.lab.es.demo.common;

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

public class ResFactory implements Serializable {

	private static final long serialVersionUID = 1L;

	private ResFactory() {
	}

	public static ResFactory getInstance() {
		return Inner.instance;
	}

	private static class Inner {
		private static final ResFactory instance = new ResFactory();
	}

	public Res getEmptyRes() {
		Res res = new Res() {
			private static final long serialVersionUID = 1L;
		};
		return res;
	}

	public ResPaging getResPaging(List<?> records, int pageNo, int pageSize, int total) {
		ResPaging resPaging = new ResPaging();
		resPaging.setRecords(records);

		resPaging.setPageNo(pageNo);
		resPaging.setPageSize(pageSize);
		resPaging.setTotal(total);

		int pageCount = total/pageSize;
		resPaging.setPageCount(pageCount);

		if (pageNo > 1) {
			resPaging.setPrevPage(String.valueOf(pageNo - 1));
		}

		if (pageNo < pageCount) {
			resPaging.setNextPage(String.valueOf(pageNo + 1));
		}
		return resPaging;
	}

	public Res getRes(String msg, int code) {
		Res res = getEmptyRes();
		res.setCode(code);
		res.setMessage(msg);
		return res;
	}

	public Res getResRecords(Object obj, String msg, int code) {
		Res res = getEmptyRes();
		res.setCode(code);
		res.setMessage(msg);
		res.setData(obj == null ? "" : obj);
		return res;
	}
}	

前端页面 index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <title>天猫搜索1111</title>
    <link rel="stylesheet" th:href="@{/css/style.css}"/>
    <link rel="stylesheet" href="//cdn.staticfile.org/element-ui/2.13.1/theme-chalk/index.css">
</head>
<body class="pg">
<div class="page" id="app">
    <div id="mallPage" class="mallist tmall- page-not-market">
        <div id="header" class=" header-list-app">
            <div class="headerLayout">
                <div class="headerCon">
                    <h1 id="mallLogo">
                        <img src="//img.alicdn.com/tfs/TB11ojWRXXXXXafaFXXXXXXXXXX-190-27.png" alt="我是一只天猫">
                    </h1>
                    <div class="header-extra">
                        <div id="mallSearch" class="mall-search">
                            <form name="searchTop" class="mallSearch-form clearfix">
                                <fieldset>
                                    <legend>天猫搜索</legend>
                                    <div class="mallSearch-input clearfix">
                                        <div class="s-combobox" id="s-combobox-685">
                                            <div class="s-combobox-input-wrap">
                                                <input v-model="keyword" type="text" autocomplete="off" value="" id="mq" class="s-combobox-input" aria-haspopup="true">
                                            </div>
                                        </div>
                                        <button type="submit" @click.prevent="handleCurrentChange(1)" id="searchbtn">
                                            搜索
                                        </button>
                                    </div>
                                </fieldset>
                            </form>
                            <ul class="relKeyTop">
                                <li><a>相关搜索1</a></li>
                                <li><a>相关搜索2</a></li>
                                <li><a>相关搜索3</a></li>
                                <li><a>相关搜索4</a></li>
                                <li><a>相关搜索5</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="content">
            <div class="main">
                <form class="navAttrsForm">
                    <div class="attrs j_NavAttrs" style="display:block">
                        <div class="brandAttr j_nav_brand">
                            <div class="j_Brand attr">
                                <div class="attrKey"> 品牌 </div>
                                <div class="attrValues">
                                    <ul class="av-collapse row-2">
                                        <li><a href="http://www.javadaily.cn" target="_blank"> 淘宝 </a></li>
                                        <li><a href="http://www.javadaily.cn" target="_blank"> 天猫 </a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="filter clearfix">
                    <a class="fSort fSort-cur">综合<i class="f-ico-arrow-d"></i></a>
                    <a class="fSort">人气<i class="f-ico-arrow-d"></i></a>
                    <a class="fSort">新品<i class="f-ico-arrow-d"></i></a>
                    <a class="fSort fSort-cur" @click="searchSort('sales')">销量<i class="f-ico-arrow-d"></i></a>
                    <a class="fSort" @click="searchSort('price')">价格<i class="f-ico-triangle-mt"></i><i class="f-ico-triangle-mb"></i></a>
                </div>

                <div class="view grid-nosku">
                    <div class="product" v-for="result in results">
                        <div class="product-iWrap">
                            <div class="productImg-wrap">
                                <a class="productImg" :href="result.tbkLink" target="_blank">
                                    <img :src="result.masterPic">
                                </a>
                            </div>
                            <p class="productPrice">
                                <em>{{result.itemPrice}}</em>
                            </p>
                            <p class="productTitle">
                                <a :title="result.itemName" v-html="result.itemNameHighlight" :href="result.tbkLink" target="_blank"></a>
                            </p>
                            <div class="productShop">
                                <span>店铺: {{result.shopName}}</span>
                            </div>
                            <p class="productStatus">
                                <span>月成交<em>{{result.monthlySales}}笔</em></span>
                                <span>评价 <a>9</a></span>
                            </p>
                        </div>
                    </div>
                </div>
                <div style="text-align: center;">
                    <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="10" layout="total, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="//cdn.staticfile.org/vue/2.6.11/vue.min.js"></script>
<script src="//cdn.staticfile.org/axios/0.19.2/axios.min.js"></script>
<script src="//cdn.staticfile.org/element-ui/2.13.1/index.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            keyword: '',sortType: '',results: [],total: 0,currentPage: 1
        },
        methods: {
            handleCurrentChange: function (val) {
                this.currentPage = val;
                var postData = {'pageNo': this.currentPage,'pageSize': 10,'keyword':this.keyword,'sort':this.sortType};

                axios.post("/search", postData).then(response => {
                    this.results = response.data.data.records;
                    this.total = response.data.data.total;
                });
            },
            searchSort(sort) {
                if (sort == 'price') {
                    if (this.sortType == 'itemPriceDown' || this.sortType == '') {
                        this.sortType = 'itemPriceUp';
                    } else {
                        this.sortType = 'itemPriceDown';
                    }
                }
                if (sort == 'sales') {
                    if (this.sortType == 'monthlySalesDown' || this.sortType == '') {
                        this.sortType = 'monthlySalesUp';
                    } else {
                        this.sortType = 'monthlySalesDown';
                    }
                }
                this.handleCurrentChange(1);
            }
        },
        mounted() {
            this.keyword = '手机';
            this.handleCurrentChange(1);
        }
    });
</script>

</body>
</html>
  • 样式文件 static/css/style.css,内容太多,此处不贴出
  • 运行 Demo
mvn spring-boot:run -Dmaven.test.skip=true

分页排序,搜索 ElasticSearch tmall_items 索引展示

image.png

  • 点击 价格 or 销量 进行排序 搜索 书籍

image.png

  • 搜索 蜂蜜 按价格 排序 分页展示

image.png


标题:AOP 更新 ElasticSearch 索引---前端数据展示
作者:shuikan95
地址:http://www.javadaily.cn/articles/2020/04/22/1587553767678.html

发表评论


TOP