1

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

}

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";
}

}

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>

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.

1 Answer 1

5

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="http://www.thymeleaf.org">
<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.