Java MVC控制器无法识别我的下拉菜单选项,应用程序无法生成
我的thymeleaf视图中有一个下拉菜单。它应该通过我的MVC控制器调用三种方法中的一种来实现排序我的不同标准——名称、评级Java MVC控制器无法识别我的下拉菜单选项,应用程序无法生成,java,model-view-controller,thymeleaf,Java,Model View Controller,Thymeleaf,我的thymeleaf视图中有一个下拉菜单。它应该通过我的MVC控制器调用三种方法中的一种来实现排序我的不同标准——名称、评级 @GetMapping public String showBeerPage(Model model) { model.addAttribute("beers", beerService.getAllBeers()); model.addAttribute("styles", styleSe
@GetMapping
public String showBeerPage(Model model) {
model.addAttribute("beers", beerService.getAllBeers());
model.addAttribute("styles", styleService.getAllStyles());
model.addAttribute("countries", countryService.getAll());
model.addAttribute("breweries", breweryService.getAll());
// for receiving user input
model.addAttribute("beerSearchDto", new BeerSearchDto());
model.addAttribute("choice");
return "beers";
}
@PostMapping("/sort")
public String sortBeers (@ModelAttribute int choice, Model model) {
model.addAttribute("styles", styleService.getAllStyles());
model.addAttribute("countries", countryService.getAll());
model.addAttribute("breweries", breweryService.getAll());
if (choice == -1) {
model.addAttribute("beers", beerService.getAllBeers());
} else if (choice == 1) {
model.addAttribute("beers", beerService.sortByName());
} else if (choice == 2) {
model.addAttribute("beers", beerService.sortByAbv());
}else if (choice == 3) {
model.addAttribute("beers", beerService.sortByRating());
}
return "beers";
}
<form th:action="@{/beers}" th:object="${choice}" method="post">
<div>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="country-ddb"
data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Sort by:
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<select th:field="${choice}" class="custom-select" >
<option class="dropdown-item" th:value="-1"
href="#">Any</option>
<option class="dropdown-item" th:value="1"
th:text="Name" href="#">Name</option>
<option class="dropdown-item" th:value="2"
th:text="ABV" href="#">ABV</option>
<option class="dropdown-item" th:value="3"
th:text="Rating" href="#">Rating</option>
</select>
</div>
</div>
</div>
</form>
。。。
第173行是:
<select th:field="${choice}" class="custom-select" >
另外,我在mvc类中将
int choice
声明为一个字段,并在构造函数中初始化它。我认为我要么没有给选择赋值,要么没有正确地将其发送到mvc控制器。有什么建议吗?我想我们没有办法帮助你。错误消息告诉您有一个名为beerController
的bean的多个定义,但是这个词在您显示的内容中没有出现。所以这个特殊的问题可能与您在这里展示的任何内容无关。请考虑提交一个,或者至少缩小你的问题,让你自己知道你向我们展示的东西和问题有什么关系。正如上面所说的,主要问题是把下拉菜单中的值传递给控制器中的方法。我对问题进行了编辑,以更好地反映我所面临的问题。我正在努力提交被问得很好的问题,并与每一个新问题一起学习。
Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringSelectFieldTagProcessor' (template: "beers" - line 173, col 37)
<select th:field="${choice}" class="custom-select" >
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'choice' available as request attribute