java Spring的bean类错误属性无效
我正在尝试一个简单的下拉菜单,在这里我用List
中的对象填充它
它返回以下错误:
Invalid property 'tests' of bean class [java.lang.String]: Bean property 'tests' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
我已经关注了其他一些教程和帖子,但仍然一无所获。我错过了什么才能让这一切成功
以下是HTML和控制器类:
<body>
<form th:object="${tests}" method='post'>
<label>Select test</label>
<select th:field="${tests}">
<option
th:each="test : ${tests}"
value="${test}"
th:text="${test}"
></option>
</select>
<input type='submit' value='Submit'>
</form>
</body>
@Controller
public class TeacherController {
TestController testcont = TestController.getInstance();
@RequestMapping(value = "/Teacher", method = RequestMethod.GET)
@ModelAttribute("tests")
//@GetMapping("/currentTestOptions")
public String currentTestOptions(Model model) {
for(Test test : testcont.showAllTests()) {
model.addAttribute(test.toString(), test);
return test.getName();
}
return "";
//return "currentTestOptions";
}
}
# 1 楼答案
你所拥有的真的没有意义
当你有一个
th:object"${tests}"
时,表达式*{tests}
表示tests.getTests()
。(我认为这不是一个有效的调用,因为test
根据您的代码是List<Test>
。)此外,所有前缀为
th:
的元素都必须是有效的表达式。有一对无效:th:value="test"
和th:text="test.getName"
应该分别为value="test"
和th:text="${test.name}"
不能将
List<>
用作表单支持对象这样的方式应该可以,但是如果你想让表单提交工作正常,你必须改变你的支持对象