目前,这些方法只返回到所需字段的自己的链接,即。可用测试的最后一个html元素仅返回应该列出所有可用测试的div中的可用测试列表。“/当前测试”和下拉框也是如此,它根本没有显示任何选项。
我从这里开始在SO上尝试一些修复,现在我的html完全崩溃了,给我错误:
模板解析过程中发生错误(模板:"模板/Teacher.html")
在java控制台中:
“BindingResult 和 Bean 名称'test'的纯目标对象都不能用作请求属性”
有什么想法吗?
下面是控制器代码,然后是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>
在控制器中,第三种方法“当前测试选项”应该返回对象的完整列表,在超文本标记语言中,我要使用test:当前测试选项遍历列表,然后作为值检索测试名称以显示在下拉列表中。
尝试打开本地页面时的当前控制台错误 /Teacher:
BindingResult 和 Bean 名称“test”的纯目标对象都不能用作请求属性
试试这段代码
<option th:each="test : ${currentTestOptions}"
th:value="${test.getName}"
th:text="${test.getName}"></option>
有关更多thymeeaf论坛/创建下拉列表
thymeef选择选项
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} 以避免获取空值。如果它为空,它会给我错误。
在html文件中有:< code >
Thymeleaf 希望您将通过模型传递称为测试
的属性。你可以这样做:
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'>