thymeleaf取model值_如何从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?

My HTML file

method="post">

name= "subjectOrder">

Select subject

th:each="Subject : ${subjects}"

th:value="${Subject}"

th:text="${Subject}">

Exam Title

Exam grade worth Submit post

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 subjects = new ArrayList();

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.

Exam 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 subjects = new ArrayList();

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

Select subject

th:each="Subject : ${subjects}"

th:value="${Subject}"

th:text="${Subject}">

Exam Title:
Exam grade worth
Submit post
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值