mysql - 使用 spring thymeleaf MVC 进行表单搜索?

标签 mysql forms spring-mvc search thymeleaf

在我的网站中,我有一个从数据库中提取的产品列表,结果通过选择/选项聚合到一个简单的列表中。现在我想通过带有“自动完成”一词的多词搜索表单来更改此设置。

这是我的选择/选项列表:

$("#Product").change(function(){
	var webProductId = $(this).find(':selected').attr('data-webProductId'); 
	var requestData = { webProductId: webProductId };
	yada.ajax([[@{/product/list}]], requestData, function(responseText, responseHtml) {
	$('div.sinVotoInner').replaceWith(responseHtml);
	yada.initHandlersOn(responseHtml);
});
<select id="wsProduct">
  <option value="Products" id="Product" selected>Product</option>
  <option th:each="webProduct : ${webProducts}"
	th:attr="data-webProductId=${webProduct.id}"
	th:text="|${webProduct.name} ${webProduct.description}|">Product 1 Description 1</option>
			</select>

这是我的家庭 Controller :

@RequestMapping("/")
	public String home(Model model) {
		
		List<WebProduct> webProducts = webProductRepository.findByEnabledTrueOrderByNameAsc();
		model.addAttribute("webProducts", webProducts);

		return "/home";
	}

这是我的存储库:

List<WebLocation> findByEnabledTrueOrderByNameAsc();

	/**
	 * @param productId
	 * @return 
	 */
	@Query(value="select * from WebProduct wp where wp.product_id = :productId", nativeQuery = true)
	WebProduct findByProductId(@Param("productId") long productId);

如何更改此代码以包含有效的搜索表单?搜索必须适用于您输入的任何字词,并且搜索建议(例如 google)会出现在搜索表单中。

最佳答案

解决办法是:

List<WebProduct> webProducts = webProductRepository.findByWord("%"+word+"%");

查询:

@Query(value="select * from WebProduct wp where wp.name like :word or wp.description like :word", nativeQuery = true)
	List<WebProduct> findByWord(@Param("word") String word);

关于mysql - 使用 spring thymeleaf MVC 进行表单搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44495756/

相关文章:

mysql - 文字不会显示

delphi - 我的 Delphi 应用程序完成初始化后,我应该在哪里放置要执行的代码?

c# - jQuery 函数来激活 onclick 方法?

eclipse - 为 Spring MVC Web 应用程序设置起始页?

java - Spring MVC 绑定(bind)重写表单输入上的 value 属性

c# - 在 C# 中调用 MySQL 例程

php - Laravel where 子句在一系列 orwhere 之后不起作用

php - 如何重定向到 PHP 中的感谢页面

java - <form :input > to map with object attribute in spring 中路径的动态值

MySQL:数据库查询不正确或结构错误?