gpt4 book ai didi

java - Thymeleaf 下拉菜单问题

转载 作者:行者123 更新时间:2023-12-02 02:39:03 25 4
gpt4 key购买 nike

我已经被这个问题困扰了一段时间,试图让一个简单的下拉菜单起作用。它填充有测试对象。这些 Test 对象存储在一个 List 中,该 List 包含在 TestController 对象中。 TestController 还有一个“activeTest”字段,这是我们要存储从下拉菜单中提交的测试的位置。它应该像这样工作:

1*从下拉菜单中选择一个测试对象2*按提交3*表单的POST方法应采用所选的Test对象并通过.setActiveTest(test)将其添加为activeTest的当前值

我有几个重复出现的错误,但现在有一个主要错误阻碍了我的进展:

“BindingResult 和 bean 名称“test”的普通目标对象都不能作为请求属性”

我知道它与 HTML View 中的第 15 行有关 (select th:field="*{test}") ,但我不知道如何解决它或它希望我修复什么。

Controller :

@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
}

最佳答案

了解Thymeleaf Variable

<select th:field="*{test}">

Test() 类中没有 test 字段

关于java - Thymeleaf 下拉菜单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57191456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com