java - Thymeleaf - 从对象列表填充下拉菜单

标签 java spring-boot thymeleaf

目前,这些方法仅返回它们自己的所需字段的链接,即。可用测试的最后一个 html 元素仅返回 div 中的 availableTestList,该列表应该列出所有可用测试。 “/currentTest”和下拉菜单相同,根本不显示任何选项。

我开始尝试从这里进行一些修复,现在我的 html 完全崩溃了,给了我错误:

模板解析时发生错误(template: "templates/Teacher.html")

在java控制台中:

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

有什么想法吗?

下面首先是 Controller 代码,然后是 html。

@Controller
public class TeacherController {

    TestController testcont = TestController.getInstance();

    @RequestMapping(value = "sendTest", method = RequestMethod.POST)
    public String sendTest(Model model) throws IOException, ServletException{

        for(Test test : testcont.showAllTests()){
            if(test.getName().equals("selection")){
                testcont.SetActiveTest(test);
                System.out.println(testcont.getActiveTest());
                //return "Test sent successfully to students! <a href='/Teacher'>Back</a>";
            }
        }
        model.addAttribute("tests", testcont.showAllTests());
        return "sendTest";
    }

    @RequestMapping(value = "resetCurrentTest", method = RequestMethod.POST)
    public String resetCurrentTest(Model model){
        testcont.SetActiveTest(null);

        model.addAttribute("tests", testcont.showAllTests());

        return "resetCurrentTest";
    }


    @RequestMapping(value = "currentTestOptions", method = RequestMethod.GET)
    //@ModelAttribute("/currentTestOptions")
    //@GetMapping("/currentTestOptions")
    public String currentTestOptions(Model model) {

        model.addAttribute("tests", testcont.showAllTests());
        return "currentTestOptions";
    }

    @RequestMapping(value = "getActiveTest", method = RequestMethod.GET)
    public String getActiveTest(){
        return testcont.getActiveTest().toString();
    }
}

HTML

<body>
    <p>
        <a href='/Teacher/NewTest'>New Test upload</a>
    </p>
    <div
        style='height: 150px; width: 400px; border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; overflow: auto;'>
        <form th:action='${sendTest}' th:object="${tests}" method='post'>
            <fieldset>
                <label>Select test</label> 
                <select id="tests" name="tests" class="form-control" th:field="${tests}">
                    <option value="">Select test</option>
                    <option 

                    th:each="test : ${tests}"
                    th:value="${test.getName}"
                    th:text="${test.getName}"

                    ></option>
                </select>
            </fieldset>
            <input type='submit' value='Submit'>
        </form>
    </div>
    <form action='${resetCurrentTest}' method='post'>
        <input type='submit' value='Clear'>
    </form>
    <a> Current Test for students: </a>
    <p th:text="${getActiveTest}" ></p>
    <p>All available tests on server:</p>
    <div
        style='height: 200px; width: 400px; border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; overflow: auto;'>
        <th:block th:each="test : ${tests}">
    </div>
</body>

在 Controller 中,第三个方法“currentTestOptions”应该返回对象的完整列表,在 HTML 中,我将使用 test : currentTestOptions 迭代列表,然后作为值检索要显示的测试名称在下拉菜单中。

尝试打开本地页面/老师时当前控制台错误是:

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

最佳答案

试试这个代码

<option th:each="test : ${currentTestOptions}"
th:value="${test.getName}"
th:text="${test.getName}"></option>

了解更多 thymeleaf-forum/Create-drop-down-list
thymeleaf-select-option

关于java - Thymeleaf - 从对象列表填充下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159860/

相关文章:

java - 使用createOntologyModel时出现异常

java - 在 spring boot 应用程序中测试 websocket

Java - 从数据库获取用户名

Thymeleaf 字符串连接

java - 在java中使用session作为登录模块有困难

java - 在 Eclipse 中搜索代码行

Spring - 使用属性扩展(processResources)并在application.yml文件中使用属性引用时发生冲突

java - Spring 启动: automatic JSON message converter for @Requestbody GET doesn't work

jquery - onChange 方法不适用于 Thymeleaf 选择选项

java - 当类的实例本身被创建时,为什么构造函数中的语句不被执行?