복붙노트

[SPRING] Spring MVC Thymeleaf 바인딩 체크 박스가있는 목록

SPRING

Spring MVC Thymeleaf 바인딩 체크 박스가있는 목록

일련의 확인란을 포함하는 thymeleaf를 사용하여 양식을 작성하려고합니다. thymeleaf 템플릿으로 전달할 객체 소스에는 String과 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 컨트롤러를 사용하여 템플릿에 유형 소스 객체를 전달합니다.

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 템플릿은 다음과 같습니다.

<!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>

내 문제는 확인란의 값이 배열에 저장되지 않는다는 것입니다. 이 코드를 실행하고 제출을 클릭하면 소스 객체의 목록이 비어 있기 때문에 널 포인터 예외가 발생합니다. sourceName은 완벽하게 작동하지만 체크 박스는 실제로 아무 것도 추가하지 않습니다.

해결법

  1. ==============================

    1.이 코드는 작동이 끝났습니다. arrayList를 포함하는 객체를 전달해야합니다. 또한 그것은 두 변수를 하나의 문자열로 결합하고 나중에 -로 구분하는 것을 도왔습니다.

    이 코드는 작동이 끝났습니다. arrayList를 포함하는 객체를 전달해야합니다. 또한 그것은 두 변수를 하나의 문자열로 결합하고 나중에 -로 구분하는 것을 도왔습니다.

    <!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>
    
  2. from https://stackoverflow.com/questions/24957738/spring-mvc-thymeleaf-binding-list-with-check-boxes by cc-by-sa and MIT license