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

时间:2019-07-23 08:28:24

标签: java spring-boot thymeleaf

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

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

模板解析过程中发生错误(模板:“ templates / Teacher.html”)

并在Java控制台中:

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

有什么想法吗?

下面是控制器代码,后面是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>

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

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

Bean名称“ test”的BindingResult和普通目标对象都不可用作请求属性

3 个答案:

答案 0 :(得分:1)

尝试此代码

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

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

答案 1 :(得分:0)

在html文件中,您具有: <select class="form-control" th:field="${test.getName}">

Thymeleaf希望您将通过模型传递名为test的属性。您可以这样做:

model.addAttribute("test", yourObjectRepresentingTest);

在将视图返回到html的控制器方法中执行此操作。例如:

@GetMapping("/showTests")
public String showTests(Model model) {
   // some controller logic if you need
   SampleTest sampleTest = new SampleTest(); // <- this is your backing bean object that will be bound to thymeleaf view
   model.addAttribute("test", sampleTest);

   return "showtests"; // <- this is a file name of a html containing your view
}

您可能还需要将th:object添加到您的html文件中:

<form th:action="@{/sendTest}" th:object="${test}" method='post'>

答案 2 :(得分:0)

Bolow是我的控制器代码:

ModelAndView view = new ModelAndView("view/index");
UserIdentity userIdentity = (UserIdentity) request.getSession().getAttribute(SessionConstant.ACCOUNT_SESSION_KEY);
if(userIdentity == null){
    return null;
}
List<PayBill> payBills = payBillService.getBillDetailByUserId(userIdentity.getId());
if(payBills != null && payBills.size() > 0){
    view.addObject("bill",payBills.get(0));
}
return view;

Bolow是我的html代码:

<div class="centerBox">
        <div class="centerBox1" th:if="${bill != null}">
            <p style="color:#999;">当月水费金额</p>
            <p style="color:red;font-size:40px;" th:text="${bill.paymentAmount}">100.00</p>
        </div>
        <div class="centerBox1" th:if="${bill == null}">
            <p style="color:#999;">当月水费金额</p>
            <p style="color:red;font-size:40px;">0.00</p>
        </div>
        <button type="button" onclick="btn()" class="mui-btn mui-btn-primary" style="width: 100%;border-radius: 20px;margin:30px 0px 10px 0px" data-loading-icon="mui-spinner mui-spinner-custom" >立即缴费</button>
        <a href="#" id="sfjl"><p>往期水费记录</p></a>
        <!-- image -->
        <div class="bottomBox">
            <img src="/images/bottom.png" width="100%" alt="" />
        </div>
    </div>

请注意,请使用此代码th:if =“ $ {bill!= null}以避免获得空值。如果为空,则会出现错误。