由于这个问题,他圈了一段时间,试图使一个简单的下拉菜单起作用。它装有测试对象。这些Test对象存储在一个List中,该List包含在TestController对象中。 TestController也有一个“ activeTest”字段,我们要从下拉菜单中存储提交的测试。它应该像这样工作:
1 *从下拉菜单中选择一个测试对象 2 *按提交 表格的3 * POST方法应采用所选的Test对象,并通过.setActiveTest(test)将其添加为activeTest的当前值
我有几个重复出现的错误,但是现在我有一个主要错误阻止了我的进步:
“ Bean名称'test'的BindingResult和普通目标对象都不能用作请求属性”
我知道它与HTML视图中的第15行有关,(选择th:field =“ * {test}”),但是我不知道如何解决它或它想要我解决什么。 / p>
控制器:
@ComponentScan
@Controller
public class TeacherController {
TestController testcont = TestController.getInstance();
@RequestMapping(value = {"/sendTest"}, method = RequestMethod.GET)
public String currentTestOptions(Model model) {
model.addAttribute("test", new Test());
model.addAttribute("tests", testcont.showAllTests());
model.addAttribute("currentTest", testcont.getActiveTest());
return "sendTest";
}
@RequestMapping(value = {"/sendTest"}, method = RequestMethod.POST)
public String sendTest(@ModelAttribute("test") @Valid @RequestBody Test test){
testcont.SetActiveTest(test);
return "sendTest";
}
}
HTML:
<body>
<p>
<a href='/Teacher/NewTest'>New Test upload</a> <a href='/Teacher/TestResults'>See Test Results</a>
</p>
<form id="dropdown" th:action="@{/sendTest}" th:object="${test}" method='post'>
<label>Select test</label>
<select th:field="*{test}">
<option th:each="test : ${tests}"
value="${test}"
th:text="${test.name}"></option>
<input type='submit' value='Submit'>
</form>
<a> Current test for students: </a>
<p th:text="${activeTest}" ></p>
<div>
<a>Available tests for students:</a>
<th:block th:each="Test : ${tests}">
<tr>
<td th:text="${Test.getName()}">...</td>
<td th:text="${Test.getFile().getName()}">...</td>
</tr>
</div>
</body>
测试类:
public class Test implements Serializable{
/**
*
*/
private static final long serialVersionUID = -8729209678450935222L;
private File file;
private String name;
private String question;
private String answer1;
private String answer2;
private double studentAnswer;
private List<Double> answers;
private List<Student> students;
public Test(File file, String name, String question, String answer1, String answer2) {
this.file = file;
this.name = name;
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
answers= new ArrayList<>();
students = new ArrayList<>();
}
// Getters and setters for above fields.
}
TestController类,其中包含存储所有Test对象的List:
public class TestController {
private static TestController instance = null;
private List<Test> tests;
private List<Student> students;
private Test active = null;
private TestController() {
tests = new ArrayList<>();
students = new ArrayList<>();
loadTests();
}
public static TestController getInstance() {
if (instance == null) {
instance = new TestController();
}
return instance;
}
public void SetActiveTest(Test test) {
active = test;
}
public Test getActiveTest() {
System.out.println(active);
return active;
}
public List<Test> showAllTests() {
return tests;
}
// Other methods
}