如何使用 Spring 引导插入多个单选按钮值

How to insert multiple radio button values with Spring boot

我正在制作一个带有 Spring 引导的测试网页。我想在我的 Oracle 数据库中的不同行中插入测试答案。

应用控制器:

@GetMapping("/test")
public ModelAndView viewTestPage() {
    ModelAndView mav = new ModelAndView("testpage");
    Results result = new Results();
    mav.addObject("answers", answerService.getAllAnswers());
    mav.addObject("questions", questionService.getAllQuestion());
    mav.addObject("result", result);
    return mav;
}
@PostMapping("/results/save")  
private String saveOrUpdate(@ModelAttribute("result") Results result)  
{       
resultService.saveOrUpdate(result); 
return "redirect:/";  
}

结果服务:

@Autowired
    private ResultsRepository resultRepository;
    
    public void saveOrUpdate(Results result){
        Optional<Results> optionalEvent= this.resultRepository.findById(result.getId());
        if (optionalEvent.isPresent()) {
            throw new IdIsAlreadyExists("Result with id: " + result.getId() + " is already exists");
        }
        else resultRepository.save(result);
    }  

Test.html:

<form action="#" th:action="@{/students/save}" th:object="${result}">
<table >
  <tr th:each="question : ${questions}">
    <!-- Question field -->
    <td  th:value="${question.qst_id}" th:text="${question.qst_title}"></td>
    <!-- Answer field -->
    <td th:text="${answers[0].answ_title}"></td>
    <td>
        <table >
            <tr>
            <td>1<input type="radio" th:name="${question.qst_id}" th:value="${answers[0].answ_id}"></td>
            <td>2<input type="radio" th:name="${question.qst_id}" th:value="${answers[1].answ_id}"></td>
            <td>3<input type="radio" th:name="${question.qst_id}" th:value="${answers[2].answ_id}"></td>
            <td>4<input type="radio" th:name="${question.qst_id}" th:value="${answers[3].answ_id}"></td>
            <td>5<input type="radio" th:name="${question.qst_id}" th:value="${answers[4].answ_id}"></td>
            </tr>
        </table>
    </td>
    <td th:text="${answers[4].answ_title}"></td>
  </tr>
  <tr>
    <td><button type="submit">Save</button></td>
  </tr>
</table>
</form>

测试页面:

test looks like

数据库架构:

student table

answers table

questions table

should insert 10+ question like these rows <-- 例如手动插入这些行

对于插入 student_id 我会使用像“${#autentication.getName()}”这样的用户名

您可以使用 form:radiobutton 或 form:radiobuttons 标签实现多个(即两个以上)单选按钮

  1. 将 form:radiobuttons 与数组列表一起使用-
<form:radiobuttons path="passFailStatus" items="${passFailStatusList}" />

其中 passFailStatusLis 是一个数组列表。

  1. 将 form:radiobuttons 与哈希映射一起使用