1.
< Script type = "text / JavaScript" > var Vue new new VM = ({ EL: "#searchApp", Data: { }, Components: { // Top loading assembly lyTop: () => import ( "./ js /pages/top.js ") } }); </ Script >
Vue this example, by way of introducing import, loading another js: top.js as a partial component. top is actually top of the page navigation component, we do matter
(2) asynchronous request
Data: {
Search: {
Key: "", // keyword search page
},
goodsList: []
},
We hook function created, on page load parameter acquisition request, and recorded.
Created () {
// determines whether there is a request parameter
IF (location.search!) {
return;
}
// the request into target parameter
const = ly.parse Search (location.search.substring (. 1));
// record in the search target data in
this.search = search;
// initiation request, according to search conditions
this.loadData ();
}
Testing in the browser:
Then initiate a request, the search data.
In leyou-gateway in the CORS configuration class, add allow trusted name:
And add a gateway mapping leyou-gateway project Application.yml in:
test:
2.
-
Request mode: Post
-
Request path: / search / page, but front / search should be mapped path to the gateway, and therefore the true path mapped page, on behalf of paging query
-
Package lucky.leyou.domain; public class the SearchRequest { Private String Key; // search condition Private Integer Page; // this page Private static Final Integer = 20 is of DEFAULT_SIZE; // page size does not receive from the page, but a fixed size Private static Final Integer, DEFAULT_PAGE, =. 1; // default page public String getKey () { return Key; } public void setKey (String Key) { the this .key = Key; } publicThe getPage Integer () { IF (Page == null ) { return , DEFAULT_PAGE,; } // do some checking getting page, not less than. 1 return Math.max (, DEFAULT_PAGE,, Page); } public void the setPage (Page Integer) { the this = .page Page; } public Integer getSize () { return of DEFAULT_SIZE; } }
Return result: As a result of paging, generally two properties: defined before the current page data, the total number of pieces of information, we can use PageResult class
package lucky.leyou.controller; import lucky.leyou.common.domain.PageResult; import lucky.leyou.domain.Goods; import lucky.leyou.domain.SearchRequest; import lucky.leyou.service.SearchService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping public class SearchController { @Autowired private SearchService searchService; /** * 搜索商品 * * @param request * @return */ @PostMapping("page") public ResponseEntity<PageResult<Goods>> search(@RequestBody SearchRequest request) { PageResult<Goods> result = this.searchService.search(request); if (result == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return ResponseEntity.ok(result); } }
(2)service
@Autowired Private GoodsRepository goodsRepository; public PageResult <Goods> Search (the SearchRequest Request) { String Key = request.getKey (); // determine whether there is a search condition, if there is no direct return null. Search all products allowed IF (StringUtils.isBlank (key)) { return null ; } // custom query builder construct query NativeSearchQueryBuilder QueryBuilder = new new NativeSearchQueryBuilder (); // . 1, a query for full-text search key queryBuilder. withQuery (QueryBuilders.matchQuery ( "All" , Key) .operator (Operator.AND)); //2, the results provided by sourceFilter returned fields, we only need ID, SKUs, subTitle queryBuilder.withSourceFilter ( new new FetchSourceFilter ( new new String [] { "ID", "SKUs", "subTitle"}, null )); // . 3 , paging // prepare paging parameters int Page = request.getPage (); int size = request.getSize (); queryBuilder.withPageable (PageRequest.of (Page - 1 , size)); // 4, query, get the results of Page <Goods> = PageInfo the this .goodsRepository.search (queryBuilder.build ()); // package and returns the result return new new PageResult <>(pageInfo.getTotalElements (), pageInfo.getTotalPages (), pageInfo.getContent ()); }
Note the point: we want to set SourceFilter, select the result to be returned, otherwise a bunch of useless data, affect the query efficiency.
(3) Testing
Restart the search service micro, refresh the page testing:
Copy the response to the results: