Spring MVC Thymeleaf绑定列表和复选框

编程入门 行业动态 更新时间:2024-10-24 04:44:23
本文介绍了Spring MVC Thymeleaf绑定列表和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用百里香叶创建一个包含一系列复选框的表单.我要传递到百里香模板的对象源包含一个字符串和一个列表.

I am trying to create a form using thymeleaf that contains a series of checkboxes. The object source that I am passing through to the thymeleaf template contains a String and a List.

package com.controller; import java.util.List; public class Source { private String sourceName; private List<String> testList; public String getSourceName() { return sourceName; } public void setSourceName(String name) { this.sourceName = name; } public List<String> getTestList() { return testList; } public void setTestList(List<String> list) { this.testList = list; } }

我使用此MVC控制器将类型为source的对象传递到模板中.

I pass an object of type source into the template using this MVC controller.

package com.controller; import java.io.IOException; import java.util.List; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.View; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import com.web_application.AllEnvironmentsFromFile; import com.web_application.AllTestsAndPaths; import com.web_application.RunDao; import com.web_application.TestAndPath; @RestController public class ManualTestController { @Autowired private ThymeleafViewResolver resolver; @Autowired RunDao rDao; @Autowired AllEnvironmentsFromFile environments; @RequestMapping(value="/manualTest", method=RequestMethod.GET) public View greetingForm(Model model) throws Exception { AllTestsAndPaths a = new AllTestsAndPaths(); List<TestAndPath> testList = a.testsAndPaths(); String[] environmentList = new String[environments.getEnvironments().size()]; for(int i = 0; i < environments.getEnvironments().size(); i++) { environmentList[i] = environments.getEnvironments().get(i).getName(); } model.addAttribute("testList", testList); model.addAttribute("source", new Source()); model.addAttribute("environmentList", environmentList); return resolver.resolveViewName("manualTest", Locale.US); } @RequestMapping(value="/manualTest", method=RequestMethod.POST) public String greetingSubmit(@ModelAttribute Source source, Model model) { System.out.println(source.getSourceName()); for(String hello : source.getTestList()) { System.out.println(hello); } model.addAttribute("source", source); return "result"; } }

manualTest模板如下所示

The manualTest template looks like this

<!DOCTYPE html> <html> <head> <title>Insert title here</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form action="#" th:action="@{/manualTest}" th:object="${source}" method="post"> <p>Source: <input type="text" th:field="*{sourceName}" /></p> <table border="1"> <tr> <td>Test Name</td> <td th:each="environment : ${environmentList}" th:text="${environment}">Tests</td> </tr> <th:block th:each="test : ${testList}"> <tr> <td th:text="${test.name}">A Test'</td> <th:block th:each="enviro : ${environmentList}"> <td><input type="checkbox" path="${testList}" value="hello" /></td> </th:block> </tr> </th:block> </table> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html>

我的问题是该复选框的值未存储在数组中.当我运行此代码并单击Submit时,我得到一个空指针异常,因为源对象中的列表为空. sourceName可以正常工作,但是复选框实际上并未添加任何内容.

My problem is that the values of the checkbox are not being stored in the array. When i run this code and click submit i get a null pointer exception because the list in the source object is empty. The sourceName works perfectly but the checkboxes are not actually adding anything.

推荐答案

该代码最终可以正常工作,您需要传入一个包含arrayList的对象.将两个变量组合成一个字符串,然后用-分隔它们也有帮助.

THis code ended up working, You need to pass in an object containing an arrayList. Also it helped that I combined two variables into one string and i later separated them by -.

<!DOCTYPE html> <html xmlns:th="www.thymeleaf"> <head> <title>Insert title here</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form action="#" th:action="@{/manualTest}" th:object="${source}" method="post"> <table border="1"> <tr> <td>Test Name</td> <td th:each="environment : ${environmentList}" th:text="${environment}">Tests</td> </tr> <th:block th:each="test : ${testList}"> <tr> <td th:text="${test.name}">A Test'</td> <th:block th:each="enviro : ${environmentList}"> <td><input type="checkbox" th:field="*{testList}" th:value="|${test.name}-${enviro}|" /></td> </th:block> </tr> </th:block> </table> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html>

更多推荐

Spring MVC Thymeleaf绑定列表和复选框

本文发布于:2023-11-24 21:15:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   复选框   列表   Spring   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!