英文:
Checkboxes parameters in the Controller?
问题
I have several checkboxes, how can I get their parameters in the controller?
我有多个复选框,如何在控制器中获取它们的参数?
I tried to write something in the controller, but it doesn't work.
我尝试在控制器中编写一些内容,但没有成功。
Controller
控制器
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping(value = "create/")
public String create(Model model) {
RegulatoryForm regulatoryform = new RegulatoryForm();
ActHasForm acthasform = new ActHasForm();
FormFrequency formFrequency = new FormFrequency();
List
List
model.addAttribute("actlist", actlist);
model.addAttribute("regulatoryform", regulatoryform);
model.addAttribute("acthasform", acthasform);
model.addAttribute("formFrequency", formFrequency);
model.addAttribute("formfrequencylist", formfrequencylist);
return "regulatoryform/create";
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping(value = "create/")
public String create(@ModelAttribute("regulatoryform") RegulatoryForm regulatoryform, @ModelAttribute("acthasform") ActHasForm acthasform, @ModelAttribute("formFrequencylist") List
FormHasFrequency formHasFrequency = new FormHasFrequency();
atiServiceImpl.saveRegulatoryForm(regulatoryform);
acthasform.setFormId(regulatoryform);
atiServiceImpl.saveActHasForm(acthasform);
for (FormFrequency form : formFrequencylist) {
formHasFrequency.setFormId(regulatoryform);
formHasFrequency.setFrequencyId(form);
atiServiceImpl.saveFormHasFrequency(formHasFrequency);
}
return "redirect:/regulatoryform/";
}
<details>
<summary>英文:</summary>
I have several checkboxes, how can I get their parameters in the controller?
I tried to write something in the controller, but it doesn't work
my html.
<input type="checkbox" th:each="formfrequencylist : ${formfrequencylist}" th:value="${formfrequencylist.id}" th:text="${formfrequencylist.frequencyNm}"
th:field="${formFrequency.id}" >
Controller
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping( value = "create/")
public String create( Model model) {
RegulatoryForm regulatoryform=new RegulatoryForm();
ActHasForm acthasform=new ActHasForm();
FormFrequency formFrequency = new FormFrequency();
List<RegulatoryAct> actlist=atiServiceImpl.findAllRegulatoryAct();
List<FormFrequency> formfrequencylist=atiServiceImpl.findAllFormFrequency();
model.addAttribute("actlist",actlist);
model.addAttribute("regulatoryform",regulatoryform);
model.addAttribute("acthasform",acthasform);
model.addAttribute("formFrequency",formFrequency);
model.addAttribute("formfrequencylist",formfrequencylist);
return "regulatoryform/create";
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping(value = "create/")
public String create(@ModelAttribute("regulatoryform") RegulatoryForm regulatoryform,@ModelAttribute("acthasform")ActHasForm acthasform,@ModelAttribute("formFrequencylist")List <FormFrequency> formFrequencylist) {
FormHasFrequency formHasFrequency=new FormHasFrequency();
atiServiceImpl.saveRegulatoryForm(regulatoryform);
acthasform.setFormId(regulatoryform);
atiServiceImpl.saveActHasForm(acthasform);
for (FormFrequency form:formFrequencylist) {
formHasFrequency.setFormId(regulatoryform);
formHasFrequency.setFrequencyId(form);
atiServiceImpl.saveFormHasFrequency(formHasFrequency);
}
return "redirect:/regulatoryform/";
}
</details>
# 答案1
**得分**: 0
I have solved my problem
```html
<th:block th:each="frequency : ${formfrequencylist}">
<input type="checkbox"
name="cers"
th:value="${frequency.id}" />
<label th:text="${frequency.frequencyNm}"></label>
</th:block>
In my Entity I added
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(name="form_has_frequency", joinColumns = @JoinColumn(name="form_id"), inverseJoinColumns = @JoinColumn(name="frequency_id"))
private List<FormFrequency> formFrequency = new ArrayList<FormFrequency>();
And my controller
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping(value = "create/")
public String create(@ModelAttribute("regulatoryform") RegulatoryForm regulatoryform, @ModelAttribute("acthasform") ActHasForm acthasform, @RequestParam(value = "cers", required = false) int[] cers) {
System.out.println("начинаем вывод" + regulatoryform);
regulatoryform.getFormFrequency().forEach(o->System.out.println(o));
atiServiceImpl.saveRegulatoryForm(regulatoryform);
acthasform.setFormId(regulatoryform);
atiServiceImpl.saveActHasForm(acthasform);
if (cers != null) {
FormFrequency formFrequency = null;
for (int i = 0; i < cers.length; i++) {
formFrequency = new FormFrequency();
formFrequency.setId(cers[i]);
regulatoryform.getFormFrequency().add(formFrequency);
}
for (int a = 0; a < regulatoryform.getFormFrequency().size(); a++) {
FormHasFrequency formHasFrequency = new FormHasFrequency();
formHasFrequency.setFormId(regulatoryform);
formHasFrequency.setFrequencyId(regulatoryform.getFormFrequency().get(a));
atiServiceImpl.saveFormHasFrequency(formHasFrequency);
}
}
return "redirect:/regulatoryform/";
}
英文:
I have solved my problem
my html
<th:block th:each="frequency : ${formfrequencylist}">
<input type="checkbox"
name="cers"
th:value="${frequency.id}" />
<label th:text="${frequency.frequencyNm}"></label>
</th:block>
In my Entity i added
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(name="form_has_frequency",joinColumns = @JoinColumn(name="form_id"),inverseJoinColumns = @JoinColumn(name="frequency_id"))
private List<FormFrequency> formFrequency=new ArrayList<FormFrequency>();
And my controller
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping(value = "create/")
public String create(@ModelAttribute("regulatoryform") RegulatoryForm regulatoryform,@ModelAttribute("acthasform")ActHasForm acthasform,@RequestParam(value = "cers" , required = false) int[] cers ) {
System.out.println("начинаем вывод"+ regulatoryform);
regulatoryform.getFormFrequency().forEach(o->System.out.println(o));
atiServiceImpl.saveRegulatoryForm(regulatoryform);
acthasform.setFormId(regulatoryform);
atiServiceImpl.saveActHasForm(acthasform);
if(cers != null)
{ FormFrequency formFrequency = null ;
for (int i = 0; i < cers.length; i++) {
formFrequency = new FormFrequency();
formFrequency.setId(cers[i]);
regulatoryform.getFormFrequency().add(formFrequency);
}
for (int a = 0; a < regulatoryform.getFormFrequency().size(); a++) {
FormHasFrequency formHasFrequency=new FormHasFrequency();
formHasFrequency.setFormId(regulatoryform);
formHasFrequency.setFrequencyId(regulatoryform.getFormFrequency().get(a));
atiServiceImpl.saveFormHasFrequency(formHasFrequency);
}
}
return "redirect:/regulatoryform/";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论