【发布时间】:2020-09-27 02:37:06
【问题描述】:
我有一个表格,我需要在其中显示许多问题和多个答案。我正在使用带有 Thymeleaf 的 spring boot 并循环显示一个列表,该列表显示问题的标签和多个选择的单选按钮。当我设置:字段属性来捕获用户输入时,它会替换所有单选按钮的名称属性。在这种情况下,我只能为所有问题选择一个单选按钮。我复制了它看起来像 thymeleaf 库中的错误。有人可以建议面对同样的事情吗?
我创建了一个示例应用程序。 Repo 是在 github 上创建的。
https://github.com/ashokruhela/thymeleaf-radio.git
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http:www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Contest</title>
</head>
<body>
<h1>contest questions</h1>
<form action="@{/result}" th:object="${contest}">
<div th:each="ques,qs: ${contest.questions}">
<label th:text="${ques.title}"></label>
<div th:each="option,os:${ques.options}">
<input type="radio"
th:field="*{ques.answer}"
th:name="${'question'+qs.count}"
th:id="${'Q'+qs.count+'opt'+os.count}"
th:value="${option.descpription}" />
</div>
</div>
</form>
</body>
</html>
比赛
package com.ak.learning.hibernatemapping;
import java.util.List;
public class Contest {
String name;
List<ContestQuestion> questions;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ContestQuestion> getQuestions() {
return questions;
}
public void setQuestions(List<ContestQuestion> questions) {
this.questions = questions;
}
}
问题
package com.ak.learning.hibernatemapping;
import java.util.List;
public class ContestQuestion {
String title;
String answer;
List<QuestionOption> options;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<QuestionOption> getOptions() {
return options;
}
public void setOptions(List<QuestionOption> options) {
this.options = options;
}
}
选项
package com.ak.learning.hibernatemapping;
public class QuestionOption {
String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
控制器
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ContestController {
@GetMapping("/contest")
public String contest(Model model){
Contest contest = new Contest();
//adding the questions is ommitted.....
model.addAttribute("contest", contest);
return "contest";
}
}
【问题讨论】:
标签: spring-boot radio-button thymeleaf