【问题标题】:How to I get the user selected value from a drop-down box and add it to the model? [duplicate]如何从下拉框中获取用户选择的值并将其添加到模型中? [复制]
【发布时间】:2018-03-01 15:53:06
【问题描述】:

我有一个用户正在创建新考试的表单,用户在表单中从下拉菜单中选择一个主题。此下拉列表包含主题字符串,而不是实际的主题对象。在我的程序中,有一些实际的主题对象与考试有一对多的关系。

如何找到用户选择的值?我想将它添加到模型中,以便我可以创建一个考试对象并将其添加到数据库中。

一旦用户选择的主题字符串被添加到模型中,如何 如何找到用户选择的值? 因为我想将他们选择的内容设置为等于主题名称,然后搜索 具有相同名称的主题的主题存储库,以便考试 可以添加到该主题。

希望我的代码能让这一点更加清晰。

我收到此错误。 bean 名称主题的绑定结果和普通目标对象都不能用作请求属性。

这是我的控制器的代码

@GetMapping("/addexam")
public String showExamForm(Model model) {

    // Here I am finding the subjects that belong to the current user 

  //and adding them as strings to an arraylist. 


  //I populate the dropdown with strings fromthis arraylist.        


    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());
    model.addAttribute("subject", exam.getSubject()); 


    //I want to find the user selected value and set it to equal 
    //subjectname:
    String subjectName =;

//Here I will search the subjectRepository for the subjectName and set subject to equal the subject that was found.

    Subject subject = subjectRepository.findBySubjectName(subjectName);
 //then exam will be added to that subject as subject has a one to many relationship with exam.

    subject.addExam(exam);
subjectRepository.save(subject);

return "userProfile1";


}
 }

这是html。

<form action="#" th:action="@{/addExam}" th:object="${exam}" 
         method="post">
                     <div th:object="${subject}">
            <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>
         </div>
         <div>
                <table>
                    <tr>

                     <td><input type="text" th:field="*{examTitle}" /></td>

                    </tr>  
                    <tr>
                        <td> Exam grade worth </td>
                        <td><input th:field="*{examGradeWorth}" /></td>

                        </tr>  

【问题讨论】:

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


    【解决方案1】:

    我认为您不需要使用两个th:object。只需使用th:value

    例如,在下一个示例中,您将发送两个对象并在student 对象中设置name,在exam 对象中设置status。 (这不是您的模型的表示。这只是一个示例)。

    <form th:action="@{/addExam}" method="post">
          <input type="text" th:value="${student.name}" name="name"/>
          <input type="text" th:value="${exam.status}" name="status"/>
          <button type="submit">Go</button>
    </form>
    

    如果有一堆 exam 字段,th:object 会很有用。

    请记住,字段名称必须与 bean 字段名称匹配。

    因此,您的 HTML 或多或少看起来像这样:

    <form action="#" th:action="@{/addExam}" method="post">
          <div>
              <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>
             </div>
             <div>
                    <table>
                        <tr>
    
                         <td><input type="text" th:field="*{exam.examTitle}" /></td>
    
                        </tr>  
                        <tr>
                            <td> Exam grade worth </td>
                            <td><input th:field="*{exam.examGradeWorth}" /></td>
    
                            </tr>  
    

    让我知道它是否有效!

    【讨论】:

    • 非常感谢您的回复。我试过了,但出现以下错误:Bean name 'subject' 的 BindingResult 和普通目标对象都不能作为请求属性使用
    • 您必须在控制器方法中添加一个名为“subject”的新参数:public String addNewExam(... Exam Exam, String subject, UserRegistrationDto userDto, BindingResult result, Model model)跨度>
    • 谢谢,我刚刚尝试过,但仍然遇到同样的错误。
    • 我应该指出,当我尝试显示表单而不是提交表单时发生了错误。
    • 啊,好的。那是因为 GET 方法。尝试添加model.addAttribute("subject", "");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多