在带有请求参数的 thymeleaf 中使用搜索功能 [英] using search functionality in thymeleaf with request parameters

查看:20
本文介绍了在带有请求参数的 thymeleaf 中使用搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,可以在其中获取条目列表.现在,我希望能够从这些列表中进行搜索.

我当前用于检索列表的网址是/show/products.我想在此页面中添加一个搜索表单,以便我可以使用请求参数进行搜索.是的,我可以使用 ajax,但我必须使用请求参数.

因此,如果我搜索产品名称,则 -/show/products?name=someName

<div class="row m-b"><div class="col-sm-6">按名称搜索:<input id="filter" type="text" th:field="*{pName}" class="form-control input-sm w-auto inline m-r"/><button class="md-btn md-fab m-b-sm indigo"><i class="material-icons md-24">&#xe8b6;</i>

</表单>

这就是我在控制器中尝试的:

@GetMapping("/show/products")公共字符串getProduct(模型模型,@RequestParam(required = false) 字符串名称,@ModelAttribute 字符串 pName) {列表<产品>产品 = this.productService.getAllProducts(name)model.addAttribute("产品", 产品);返回show_product";}

我收到此错误:

 BindingResult 和 bean 名称pName"的普通目标对象都不能用作请求属性在 org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153)在 org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:897)

解决方案

您正在尝试将变量 pName(模型属性)用作表单对象.

在您的视图中,您将模型属性传递给表单,就像这样 th:object="${pName}" 但您需要传递一个表单对象.

表单对象不是一个类,而是一个简单的 Java 对象 (POJO).您可以将表单对象视为您的表单,但位于服务器端.

在视图中使用表单对象之前,您需要创建它并将其添加到模型中.

你会像这样定义它

公共类 MyFormObject{私人字符串 pName;公共字符串 getPName(){返回 pName;}public void setPName(String pName){this.pName = pName;}}

现在你的控制器方法将变成

@GetMapping("/show/products")公共字符串getProduct(模型模型,@ModelAttribute("myFormObject") MyFormObject myFormObject,BindingResult 结果) {列表<产品>产品 = this.productService.getAllProducts(myFormObject.getPName());model.addAttribute("产品", 产品);返回show_product";}

然后你可以像这样将表单对象传递给你的表单

 <div class="row m-b"><div class="col-sm-6">按名称搜索:<input id="filter" type="text" th:field="*{pName}" class="form-control input-sm w-auto inline m-r"/><button class="md-btn md-fab m-b-sm indigo"><i class="material-icons md-24">&#xe8b6;</i></button>

</表单>

你需要阅读文档,所有这些都有详细的解释.

I have a page where I get a list of entries. Now, I want to be able to search from those list.

my current url for retrieving list is this /show/products. I want to add a search form in this page so that I can search with request parameter. Yes, I can use ajax but I have to do it with request parameters.

So if I search for a product name, then - /show/products?name=someName

<form ui-jp="parsley" th:action="@{/show/products(name=${pName})}" th:object="${pName}" method="get">
    <div class="row m-b">
        <div class="col-sm-6">
            Search by Name:
            <input id="filter" type="text" th:field="*{pName}" class="form-control input-sm w-auto inline m-r"/>
            <button class="md-btn md-fab m-b-sm indigo">
                <i class="material-icons md-24">&#xe8b6;</i>
            </button>
        </div>
    </div>
</form>

And this is what I tried in controller:

@GetMapping("/show/products")
public String getProduct(Model model,
                         @RequestParam(required = false) String name,
                         @ModelAttribute String pName) {

    List<Product> products = this.productService.getAllProducts(name)
    model.addAttribute("products", products);
    return "show_product";
}

I am getting this error:

Neither BindingResult nor plain target object for bean name 'pName' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153)
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:897)

解决方案

You are trying to use variable pName (Model attribute) as a form object.

In your view you are passing a model attribute to form like this th:object="${pName}" but instead you need to pass a form object.

A form object is not a class but rather a simple java object (POJO). You can think of form object as your form but on server side.

Before you can use form object in your view, you need to create it and add it to the Model.

you will define it like this

public class MyFormObject{
    private String pName;
    public String getPName(){
        return pName;
    }
    public void setPName(String pName){
        this.pName = pName;
    }
}

now your controller method will become

@GetMapping("/show/products")
public String getProduct(Model model,
    @ModelAttribute("myFormObject") MyFormObject myFormObject,
    BindingResult result) {
    List<Product> products = this.productService.getAllProducts(myFormObject.getPName());
    model.addAttribute("products", products);
    return "show_product";
}

Then you can pass the form object to your form like this

                       <form ui-jp="parsley" th:action="@{/show/products}" th:object="${myFormObject}" method="get">
                            <div class="row m-b">
                                <div class="col-sm-6">
                                    Search by Name: <input id="filter" type="text" th:field="*{pName}" class="form-control input-sm w-auto inline m-r"/>
                                    <button class="md-btn md-fab m-b-sm indigo"><i class="material-icons md-24">&#xe8b6;</i></button>
                                </div>
                            </div>
                        </form>

You need to read the documentation, all these are explained there in detail.

这篇关于在带有请求参数的 thymeleaf 中使用搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆