java - 如何使用 Thymeleaf 从下拉菜单中获取所选值?

标签 java spring spring-boot drop-down-menu thymeleaf

我的用户正在添加一个考试对象,然后将其添加到主题对象中。科目和考试是一对多的关系。 用户正在下拉菜单中选择主题。该菜单包含字符串而不是实际的主题对象。 在这种形式中,如何将检查对象和所选项目(字符串)发送到 Controller ?

我的 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>

Controller ,我想将主题名称设置为等于用户在下拉框中选择的主题。

    @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 :

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

关于java - 如何使用 Thymeleaf 从下拉菜单中获取所选值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999378/

相关文章:

spring - 如何从 Spring 检索 azure AD JWT 访问 token ?

spring-mvc - 在 Spring Boot 中使用 StandardPasswordEncoder

java - 使用Spring boot渲染jsp页面出错?

java - 我需要将 PDDocument 转换为文件对象

java - 使用 ArrayDeque 实现缓存

java - char和int数组的区别

java - 对象作为没有哈希码和等于的映射键

java - Spring Boot 应用程序 Tomcat 服务器未运行

spring - Spring Entity Manager 和 Spring Data Repository 有什么区别?

java - 重新启动应用程序时事件处理程序重播?