如何从Thymeleaf的下拉菜单中获取所选值? [英] How do I get the selected value from a drop-down menu with Thymeleaf?

查看:1505
本文介绍了如何从Thymeleaf的下拉菜单中获取所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户正在添加检查对象,然后将其添加到主题对象。科目和考试具有一对多的关系。
用户正在下拉菜单中选择主题。此菜单包含的字符串不是实际的主题对象。
在此表单中,如何将检查对象和所选项目(字符串)发送到控制器?

My user is adding an exam object which is then added to the subject object. Subject and exam have a one to many relationship. The user is selecting the subject in the drop down menu.This menu contains string not actual subject objects. In this form, how do I send an exam object and the selected item (String) to the 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>

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

The controller, I want to set subject Name to equal the subject the user selected in the drop down box.

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


}


推荐答案

<我设法找到了选择的值。
请参阅下面的代码。

I managed to find the selected value. See my code below.

检查控制器:

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

这篇关于如何从Thymeleaf的下拉菜单中获取所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆