如何根据我在多个下拉列表中选择的内容创建过滤器?

时间:2018-10-26 11:34:33

标签: spring spring-mvc spring-boot thymeleaf

我大约有15个下拉菜单,我想返回数据库中与下拉菜单中选择的对象相等的对象。我设法通过Thymeleaf和Spring在下拉列表中显示数据,然后发出了POST请求,并在其中收到了所选内容。我不知道如何使用jpa处理仓库中从数据库中获得对象的方法。

控制器

@GetMapping("/filter/functionality/misramessages")
    public String filterFunctionality(Model model) {

        model.addAttribute("misraMessages", misraMessagesService.findAllMisraMessagesFromDb());
        model.addAttribute("mistraObjectSelected", new MisraMessages());

        return "functionality";
    }

    @PostMapping("/filter/functionality/misramessages")
    public String getFilterFunctionalityResult(@PathVariable(name = "buidId", required=false) Build buildId,
                                               @RequestParam(name = "fileName", required=false) String fileName,
                                               @RequestParam(name = "filePath", required=false) String filePath,
                                               @RequestParam(name = "messageNumber", required=false) String messageNumber,
                                               @RequestParam(name = "messageSeverity", required=false) String messageSeverity,
                                               @RequestParam(name = "guidelineNumber", required=false) String guidelineNumber,
                                               @RequestParam(name = "guidelineName", required=false) String guidelineName,
                                               @RequestParam(name = "guidelineType", required=false) String guidelineType,
                                               @RequestParam(name = "guidelineSeverity", required=false) String guidelineSeverity,
                                               @RequestParam(name = "requirement", required=false) String requirement,
                                               @RequestParam(name = "sourceCodeCategory", required=false) String sourceCodeCategory,
                                               @RequestParam(name = "safety", required=false) String safety,
                                               @RequestParam(name = "functionality", required=false) String functionality,
                                               @RequestParam(name = "subFunctionality", required=false) String subFunctionality,
                                               @RequestParam(name = "applicationType", required=false) String applicationType,
                                               @RequestParam(name = "supplier", required=false) String supplier,
                                               @RequestParam(name = "cmVersion", required=false) String cmVersion,
                                               @RequestParam(name = "asilLevel", required=false) String asilLevel) {


 }

下拉菜单

 <form action="#" th:action="@{/filter/result}" method="post">

        <!-- Filter Build-->
        <div class="form-group row">
            <label for="build" class="col-sm-2 col-form-label">Build</label>
            <div class="col-sm-10">
                <select class="form-control"  name="buildId" id="build">
                    <option th:value="0" text="Please Select"></option>
                    <option  th:each = "misra : ${misraMessages}"
                             th:value="${misra.buildId.buildId}"
                             th:text="${misra.buildId.buildId}">
                    </option>
                </select>
            </div>
        </div>

        <!-- Filter File Name-->
        <div class="form-group row">
            <label for="fileName" class="col-sm-2 col-form-label">File Name</label>
            <div class="col-sm-10">
                <select class="form-control"  name="fileName" id="fileName">
                    <option th:value="0" text="Please Select"></option>
                    <option  th:each = "misra : ${misraMessages}"
                             th:value="${misra.fileName}"
                             th:text="${misra.fileName}">
                    </option>
                </select>
            </div>
        </div>
           ..........
           ..........
           ..........
</form>

1 个答案:

答案 0 :(得分:1)

由于您的问题过于笼统,因此我无法为您提供具体答案,但我会尽力为您提供一般性想法。

首先,您需要了解JPA的工作方式。确定您要用于例如。休眠,春季数据,jdbc,mybatis等...您可以在Internet上找到很多有关如何使用JPA的教程。

首先,您需要创建一个具有所有属性的POJO类。这个POJO类是这样的

public class MyClass{

private String filename;
private String filePath;
...
//getters setters
}

我会将所有RequestParam合并为map。这不是必需的,但是可以使代码整洁,因为您有很多RequestParam

 @PostMapping("/filter/functionality/misramessages")
        public String getFilterFunctionalityResult(@PathVariable(name = "buidId", required=false) Build buildId,@RequestParam Map<String,String> requestParams){
// your code here
}

现在您可以将所有RequestParam的值设置为POJO

Myclass myClass = new Myclass();
myclass.setFileName(requestParams.get("fileName"));
...

您必须创建@Service@Repository类,这些类将处理所有服务层逻辑以及与数据库交互的逻辑。 spring.io上有一些很好的例子。尝试在那里度过一段时间。

希望这会有所帮助。