我在一个简单的 Spring Boot 应用程序中遇到错误。
我的错误: Bean属性'menu'无法读取或getter方法无效:getter的返回类型是否与setter的参数类型匹配?
当我点击 Thymeleaf 视图中的“添加菜单”按钮时,会出现问题。 我在网上搜索了可能的解决方案,即使它看起来像是一个直接的bug,我也不知道如何管理它。
我多次更改并检查了Menu类,但我没有发现问题。
该应用程序用于创建带有奶酪的自定义菜单。
MenuController:
package com.ghiurutan.springdemo.controllers;
import com.ghiurutan.springdemo.data.CheeseDAO;
import com.ghiurutan.springdemo.data.MenuDAO;
import com.ghiurutan.springdemo.models.Cheese;
import com.ghiurutan.springdemo.models.Menu;
import com.ghiurutan.springdemo.models.forms.AddMenuItemForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
@Controller
@RequestMapping("menu")
public class MenuController {
@Autowired
private MenuDAO menuDAO;
@Autowired
private CheeseDAO cheeseDAO;
@RequestMapping(value = "")
public String index(Model model) {
model.addAttribute("title", "Menus");
model.addAttribute("menus", menuDAO.findAll());
return "menu/index";
}
@RequestMapping(value = "add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("title", "Add Menu");
model.addAttribute(new Menu());
return "menu/add";
}
@RequestMapping(value = "add", method = RequestMethod.POST)
public String add(Model model, @ModelAttribute @Valid Menu menu, Errors errors) {
if (errors.hasErrors()) {
model.addAttribute("title", "Add Menu");
model.addAttribute(new Menu());
return "menu/add";
}
menuDAO.save(menu);
return "redirect:view/" + menu.getId();
}
@RequestMapping(value = "view/{menuId}", method = RequestMethod.GET)
public String viewMenu(Model model, @PathVariable int menuId) {
Menu menu = menuDAO.findOne(menuId);
model.addAttribute("title", menu.getName());
model.addAttribute("cheeses", menu.getCheeses());
model.addAttribute("menuId", menu.getId());
return "menu/view";
}
@RequestMapping(value = "add-item/{menuId}", method = RequestMethod.GET)
public String addItem(Model model, @PathVariable int menuId) {
Menu menu = menuDAO.findOne(menuId);
AddMenuItemForm form = new AddMenuItemForm(cheeseDAO.findAll(), menu);
model.addAttribute("title", "Add item to menu: " + menu.getName());
model.addAttribute("form", form);
return "menu/add-item";
}
@RequestMapping(value = "add-item", method = RequestMethod.POST)
public String addItem(Model model, @ModelAttribute @Valid AddMenuItemForm form, Errors errors) {
if (errors.hasErrors()) {
model.addAttribute("form", form);
model.addAttribute("title", "Add item to menu: " + form.getMenu().getName());
return "menu/add-item";
}
Cheese cheese = cheeseDAO.findOne(form.getCheeseId());
Menu menu = menuDAO.findOne(form.getMenuId());
menu.addItem(cheese);
menuDAO.save(menu);
return "redirect:/menu/view/" + menu.getId();
}
}
菜单模型类:
package com.ghiurutan.springdemo.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
@Entity
public class Menu {
@Id
@GeneratedValue
private int id;
@NotNull
@Size(min = 3, max = 15)
private String name;
@ManyToMany
private List<Cheese> cheeses;
public Menu() {
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addItem(Cheese item) {
cheeses.add(item);
}
public List<Cheese> getCheeses() {
return cheeses;
}
}
我按下添加菜单按钮的百里叶视图:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">
<h1 th:text="${title}">Default title</h1>
<nav th:replace="fragments :: navigation"></nav>
<ul>
<li th:each="menu : ${menus}">
<a th:href="'/menu/view/' + ${menu.id}" th:text="${menu.name}"></a>
</li>
</ul>
<p>
<a href="/menu/add">Add Menu</a>
</p>
</body>
</html>
在尝试渲染时崩溃的百万美元视图模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">
<h1 th:text="${title}">Default Title</h1>
<nav th:replace="fragments :: navigation"></nav>
<form method="post" style="max-width: 600px" th:object="${menu}">
<div class="form-group">
<label th:for="name">Name</label>
<input class="form-control" th:field="*{menu}"/>
<span th:errors="*{name}" class="error"></span>
</div>
<input type="submit" value="Create Menu"/>
</form>
</body>
</html>
在网页中出现:执行处理器'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'时出错(菜单/添加:12)
第12行是:<input class="form-control" th:field="*{menu}"/>
非常感谢!