Spring Boot 控制器中的复选框参数?

tvmytwxo  于 2023-06-05  发布在  Spring
关注(0)|答案(1)|浏览(225)

我有几个复选框,我如何在控制器中获得它们的参数?我试着在控制器里写点东西,但是不起作用

my html.
<input type="checkbox" th:each="formfrequencylist : ${formfrequencylist}" th:value="${formfrequencylist.id}" th:text="${formfrequencylist.frequencyNm}"
                             th:field="${formFrequency.id}" >

控制器

@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/";
}
2vuwiymt

2vuwiymt1#

我已经解决了我的问题

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>();

还有我的控制器

@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/";
}

相关问题