【问题标题】:How do I get the selected value from a drop-down menu with Thymeleaf?如何使用 Thymeleaf 从下拉菜单中获取所选值?
【发布时间】:2018-08-06 13:13:56
【问题描述】:

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

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


}

【问题讨论】:

  • @Cata 是的,我是故意这样做的,因为我非常渴望帮助,我已经被困在这个问题上几个星期了!
  • 我认为在这种情况下一个好的做法是用新信息或更详细的信息更新您的帖子。这样,问题将再次出现在顶部(据我所知......)

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


【解决方案1】:

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

考官:

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

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 2020-11-28
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    相关资源
    最近更新 更多