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

Java 如何使用Thymeleaf从下拉菜单中获取所选值?,java,spring,spring-boot,drop-down-menu,thymeleaf,Java,Spring,Spring Boot,Drop Down Menu,Thymeleaf,我的用户正在添加一个考试对象,然后将其添加到subject对象。科目和考试有一对多的关系。 用户正在下拉菜单中选择主题。此菜单包含字符串而不是实际的主题对象。 在此表单中,如何将考试对象和所选项目(字符串)发送到控制器 我的HTML文件 <form action="#" th:action="@{/addExam}" th:object="${exam}" method="post"> <div th:object=

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

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


}
@GetMapping(“/addexam”)
公共字符串showExamForm(模型){
身份验证日志用户=
SecurityContextHolder.getContext().getAuthentication();
字符串email=loggedInUser.getName();
User User=userRepository.findByEmailAddress(电子邮件);
ArrayList主题=新建ArrayList();
for(主题子:user.getSubject())
{
subjects.add(sub.getSubjectName());
}
model.addAttribute(“主题”,主题);
返回“addExam”;
}
@后映射(“/addexam”)
公共字符串addNewExam(@modeldattribute(“exam”)@Valid@RequestBody-exam
考试,UserRegistrationDto用户DTO,BindingResult结果,模型){
examRepository.save(考试);
model.addAttribute(“examTitle”,exam.getExamTitle());
model.addAttribute(“examGradeWorth”,exam.getExamGradeWorth());
字符串subjectName=();
//我想将subjectName设置为与所选选项相等。
Subject Subject=subjectRepository.findBySubjectName(subjectName);
科目。附录(考试);
subjectRepository.save(主题);
返回“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";


    }
}
@控制器
公共类加法器{
@自动连线
私家侦探;
@自动连线
私有主题库主题库;
@自动连线
私有用户存储库用户存储库;
@模型属性(“考试”)
公开考试{
返回新的考试();
}
@GetMapping(“/addexam”)
公共字符串showExamForm(模型){
身份验证loggedInUser=SecurityContextHolder.getContext().getAuthentication();
字符串email=loggedInUser.getName();
User User=userRepository.findByEmailAddress(电子邮件);
ArrayList主题=新建ArrayList();
for(主题子:user.getSubject())
{
subjects.add(sub.getSubjectName());
}
model.addAttribute(“主题”,主题);
返回“addExam”;
}
@PostMapping(“/addExam”)//这导致了我遇到的一个问题。我把它作为/addExam,它应该是addExam
公共字符串addNewExam(@modeldattribute(“exam”)@Valid@RequestBody-exam-exam,UserRegistrationDto-userDto,BindingResult-result,Model-Model){
examRepository.save(考试);
model.addAttribute(“examTitle”,exam.getExamTitle());
model.addAttribute(“examGradeWorth”,exam.getExamGradeWorth());
model.addAttribute(“主题”,“主题”);
//字符串subjectName=(“subject”,exam.getSubject());
//Subject Subject=subjectRepository.findBySubjectName(subjectName);
//科目。附录(考试);
///subjectRepository.save(主题);
返回“userProfile1”;
}
}
HTML


选择主题
考试题目:
考试成绩价值
投递

可能重复:@Cata是的,我是故意这么做的,因为我非常需要帮助,我已经在这上面呆了两个星期了!我认为在这种情况下,一个好的做法是用新的信息或更详细的信息更新你的帖子。这样,问题将再次出现在顶部(据我所知…)
    <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>