【问题标题】:Form search with spring thymeleaf MVC?使用spring thymeleaf MVC进行表单搜索?
【发布时间】:2017-11-13 17:06:03
【问题描述】:

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

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

$("#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>

这是我的家庭控制器:

@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)会出现在搜索表单中。

【问题讨论】:

  • 搜索建议将使用 javascript + ajax 调用来完成。我看不出在这种情况下会在哪里使用百里香?您只需放置一个纯文本框并收听键盘事件,对后端进行异步 REST 调用以获取建议

标签: mysql forms spring-mvc search thymeleaf


【解决方案1】:

解决办法是:

List&lt;WebProduct&gt; 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);

【讨论】:

    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多