如何根据我在多个下拉列表中选择的内容创建过滤器?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何根据我在多个下拉列表中选择的内容创建过滤器?相关的知识,希望对你有一定的参考价值。
我有大约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>
答案
由于您的问题太广泛,我无法为您提供具体的答案,但我会尽力给您一般的想法。
首先,您需要了解JPA
的工作原理。决定你想要用什么,例如。 hibernate,spring data,jdbc,mybatis等...你可以在互联网上找到很多关于如何使用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的完美例子。试着在那里度过一段时间。
希望这可以帮助。
以上是关于如何根据我在多个下拉列表中选择的内容创建过滤器?的主要内容,如果未能解决你的问题,请参考以下文章