我无法搜索某些情况。
示例:当我键入“ 5”时,它在HTML页面上不返回任何结果,但是当我尝试“ 1”时,它返回“ product15”吗?为什么“ 5”无法搜索?
我的错误是什么?
以及如何在URL中显示我的所有参数。当我打开'localhost:8080'时,URL中没有默认的参数,但是它们仍然运行良好。
请帮帮我!
<form class="search" th:action="@{/}" th:object="${productVM}" method="get">
<input type="search" id="search" th:field="*{name}" placeholder="search">
<button type="submit" class="search-button"><span class="icon"><i class="fa fa-search"></i></span></button>
</form>
@GetMapping(value = "")
public String home(
Model model,
@Valid @ModelAttribute("productVM")ProductVM productVM,
@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(name = "size", required = false, defaultValue = "10") Integer size,
@RequestParam(name = "sortByPrice", required = false) String sort
)
{
HomeVM homeVM = new HomeVM();
// set view category
List<Category> categoryList = categoryService.getListAllCategories();
List<CategoryVM> categoryVMList = new ArrayList<>();
for(Category category : categoryList) {
CategoryVM categoryVM = new CategoryVM();
categoryVM.setId(category.getId());
categoryVM.setName(category.getName());
categoryVMList.add(categoryVM);
}
Pageable pageable = PageRequest.of(page,size,sort1);
Page<Product> productPage = null;
if (productVM.getName() != null && !productVM.getName().isEmpty())
{
productPage = productService.getListProductByProductNameContaining(pageable,productVM.getName().trim());
}
else
productPage = productService.getListProductByProductNameContaining(pageable,null);
List<ProductVM> productVMList = new ArrayList<>();
for (Product product : productPage.getContent())
{
ProductVM productVM1 = new ProductVM();
productVM1.setId(product.getId());
productVM1.setName(product.getName());
productVM1.setPrice(product.getPrice());
productVM1.setMainImage(product.getMainImage());
productVM1.setCreatedDate(product.getCreatedDate());
productVMList.add(productVM1);
}
// set view
homeVM.setCategoryVMList(categoryVMList);
homeVM.setProductVMList(productVMList);
model.addAttribute("vm",homeVM);
model.addAttribute("page",productPage);
return "home";
}
我的存储库:
@Query("SELECT p FROM product p " +
"WHERE (:productName IS NULL OR UPPER(p.name) LIKE CONCAT('%',UPPER(:productName),'%'))")
Page<Product> getListProductByProductNameContaining(Pageable pageable, @Param("productName") String productName);
我的服务:
public Page<Product> getListProductByProductNameContaining(Pageable pageable, String productName){
return productRepository.getListProductByProductNameContaining(pageable,productName);
}