如何使用SpringBoot插入多个单选按钮值

r8uurelv  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(281)

我正在用SpringBoot做一个测试网页。我想在我的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);
    }

测试.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>

测试页面:
测试看起来像
数据库架构:
学生桌
答案表
问题表
应该像这些行那样插入10+个问题<--只是以手动插入这些行为例
对于insert student\u id,我将使用类似“${autentication.getname()}”的用户名

unguejic

unguejic1#

可以使用实现多个(即两个以上)单选按钮form:radiobutton or form:radiobuttons tag
使用form:radiobuttons with 数组列表-

<form:radiobuttons path="passFailStatus" items="${passFailStatusList}" />

其中passfailstatuslis是一个数组列表。
使用form:radiobuttons with 哈希图

相关问题