Java实现搜索功能代码详解

Java实现搜索功能代码详解

2023年7月9日发(作者:)

Java实现搜索功能代码详解⾸先,我们要清楚搜索框中根据关键字进⾏条件搜索发送的是Get请求,并且是向当前页⾯发送Get请求//⽰例代码 请求路径为当前页⾯路径 "/product"

当我们要实现多条件搜索功能时,可以将搜索条件封装为⼀个Map集合,再根据Map集合进⾏搜索Controller层代码:@GetMapping("/product") public String list(@RequestParam(required = false,defaultValue = "1",name = "p")Integer pageNo, @RequestParam(required = false,defaultValue = "")String productName, @RequestParam(required = false,defaultValue = "")String place, @RequestParam(required = false,defaultValue = "")Integer typeId, @RequestParam(required = false,defaultValue = "")BigDecimal minPrice, @RequestParam(required = false,defaultValue = "")BigDecimal maxPrice, Model model) { Map searchParam = new HashMap<>(); ("productName",productName); ("place",place); ("typeId",typeId); ("minPrice",minPrice); ("maxPrice",maxPrice); PageInfo pageInfo = PageNo(pageNo,searchParam); ribute("pageInfo",pageInfo); return "product/list"; }业务层代码:public PageInfo findByPageNo(Integer pageNo, Map searchParam) { age(pageNo,10); List kaolaList = SearchParamWithType(searchParam); return new PageInfo<>(kaolaList);}MyBatis中的:这样,就可以从前端到后端实现多条件搜索功能了。我们还会遇到这样⼀种情况,在输⼊搜索条件时,显⽰列表会不断⾃动刷新,这⾥其实⽤到了Ajax的相关内容,在输⼊的过程中,会不断发出Ajax请求,然后刷新页⾯。value="${tName}"是从请求url的参数中获取值,实现在输⼊关键字搜索后刷新页⾯显⽰关键字这⼀功能,直接上图:在输⼊中⽂关键字进⾏搜索时,可以使⽤encodeURIComponent解决url路径显⽰中⽂乱码问题://分页$('#pagination-demo').twbsPagination({ totalPages: ${}, visiblePages: 10, first:'⾸页', last:'末页', prev:'上⼀页', next:'下⼀页', href:"?productName="+encodeURIComponent('${tName}')+"&place="+encodeURIComponent('${}') + "&typeId=${}&minPrice=${ce}&maxPrice=${ce}&p={{number}}"});点击查看⼤图搜索结果总结以上所述是⼩编给⼤家介绍的Java实现搜索功能代码详解,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688892483a181724.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信