[英]How do I get the selected value from a drop-down menu with Thymeleaf?
我的用戶正在添加檢查對象,然后將其添加到主題對象。 科目和考試具有一對多的關系。 用戶正在下拉菜單中選擇主題。此菜單包含的字符串不是實際的主題對象。 以這種形式,我如何將檢查對象和所選項目(字符串)發送到控制器?
我的HTML檔案
<form action="#" th:action="@{/addExam}" th:object="${exam}"
method="post">
<div th:object="${subject}">
<select th:field="*{option}" class="form-control" id="subjectOrder"
name= "subjectOrder">
<option value="">Select subject</option>
<option
th:each="Subject : ${subjects}"
th:value="${Subject}"
th:text="${Subject}"></option>
</div>
<div>
<table>
<tr>
Exam Title
<td><input type="text" th:field="*{examTitle}" /></td>
</tr>
<tr>
<td> Exam grade worth </td>
<td><input th:field="*{examGradeWorth}" /></td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</div>
</form>
控制器,我想將主題名稱設置為等於用戶在下拉框中選擇的主題。
@GetMapping("/addexam")
public String showExamForm(Model model) {
Authentication loggedInUser =
SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userRepository.findByEmailAddress(email);
ArrayList<String> subjects = new ArrayList<String>();
for(Subject sub:user.getSubject())
{
subjects.add(sub.getSubjectName());
}
model.addAttribute("subjects", subjects);
return "addExam";
}
@PostMapping("/addexam")
public String addNewExam(@ModelAttribute("exam") @Valid @RequestBody Exam
exam,UserRegistrationDto userDto, BindingResult result, Model model) {
examRepository.save(exam);
model.addAttribute("examTitle", exam.getExamTitle());
model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
String subjectName = ();
//I want to set subjectName to equal the selected option.
Subject subject = subjectRepository.findBySubjectName(subjectName);
subject.addExam(exam);
subjectRepository.save(subject);
return "userProfile1";
}
我設法找到選定的值。 請參閱下面的代碼。
考試負責人:
@Controller
public class AddExamController {
@Autowired
private ExamRepository examRepository;
@Autowired
private SubjectRepository subjectRepository;
@Autowired
private UserRepository userRepository;
@ModelAttribute("exam")
public Exam exam() {
return new Exam();
}
@GetMapping("/addexam")
public String showExamForm(Model model) {
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userRepository.findByEmailAddress(email);
ArrayList<String> subjects = new ArrayList<String>();
for(Subject sub:user.getSubject())
{
subjects.add(sub.getSubjectName());
}
model.addAttribute("subjects", subjects);
return "addExam";
}
@PostMapping("/addExam") //This was causing one problem i was getting. I had it as /addexam and it should have been addExam
public String addNewExam(@ModelAttribute("exam") @Valid @RequestBody Exam exam,UserRegistrationDto userDto, BindingResult result, Model model) {
examRepository.save(exam);
model.addAttribute("examTitle", exam.getExamTitle());
model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
model.addAttribute("subject", "");
//String subjectName = ("subject", exam.getSubject());
// Subject subject = subjectRepository.findBySubjectName(subjectName);
//subject.addExam(exam);
/// subjectRepository.save(subject);
return "userProfile1";
}
}
HTML
<form action="#" th:action="@{/addExam}" th:object="${exam}" method="post">
<select th:field="*{subject}" class="form-control" id="subject" name="subject">
<option value="">Select subject</option>
<option
th:each="Subject : ${subjects}"
th:value="${Subject}"
th:text="${Subject}"></option>
<table>
<tr>
<td> Exam Title:</td>
<td><input type="text" th:field="*{examTitle}" /></td>
</tr>
<tr>
<td> Exam grade worth </td>
<td><input th:field="*{examGradeWorth}" /></td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</div>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.