复选框控制器中的参数?

huangapple go评论53阅读模式
英文:

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 actlist = atiServiceImpl.findAllRegulatoryAct();
List 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 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>
<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&#39;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(&quot;hasRole(&#39;ROLE_ADMIN&#39;)&quot;)
	@GetMapping( value = &quot;create/&quot;)
	public String create( Model model) {
		RegulatoryForm regulatoryform=new RegulatoryForm();
		ActHasForm acthasform=new ActHasForm();
		FormFrequency formFrequency = new FormFrequency();
		List&lt;RegulatoryAct&gt; actlist=atiServiceImpl.findAllRegulatoryAct();
	    List&lt;FormFrequency&gt; formfrequencylist=atiServiceImpl.findAllFormFrequency();
		model.addAttribute(&quot;actlist&quot;,actlist);
		model.addAttribute(&quot;regulatoryform&quot;,regulatoryform);
		model.addAttribute(&quot;acthasform&quot;,acthasform);
		model.addAttribute(&quot;formFrequency&quot;,formFrequency);
		
		model.addAttribute(&quot;formfrequencylist&quot;,formfrequencylist);
		return &quot;regulatoryform/create&quot;;
	}
	
	@PreAuthorize(&quot;hasRole(&#39;ROLE_ADMIN&#39;)&quot;)
	@PostMapping(value = &quot;create/&quot;)
	public String create(@ModelAttribute(&quot;regulatoryform&quot;) RegulatoryForm regulatoryform,@ModelAttribute(&quot;acthasform&quot;)ActHasForm acthasform,@ModelAttribute(&quot;formFrequencylist&quot;)List &lt;FormFrequency&gt; 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 &quot;redirect:/regulatoryform/&quot;;
	}



</details>


# 答案1
**得分**: 0

I have solved my problem
```html
    &lt;th:block th:each=&quot;frequency : ${formfrequencylist}&quot;&gt;
        &lt;input type=&quot;checkbox&quot;
               name=&quot;cers&quot; 
               th:value=&quot;${frequency.id}&quot; /&gt; 
        &lt;label th:text=&quot;${frequency.frequencyNm}&quot;&gt;&lt;/label&gt;
    &lt;/th:block&gt;

In my Entity I added

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    @JoinTable(name=&quot;form_has_frequency&quot;, joinColumns = @JoinColumn(name=&quot;form_id&quot;), inverseJoinColumns = @JoinColumn(name=&quot;frequency_id&quot;))
    private List&lt;FormFrequency&gt; formFrequency = new ArrayList&lt;FormFrequency&gt;();

And my controller

    @PreAuthorize(&quot;hasRole(&#39;ROLE_ADMIN&#39;)&quot;)
    @PostMapping(value = &quot;create/&quot;)
    public String create(@ModelAttribute(&quot;regulatoryform&quot;) RegulatoryForm regulatoryform, @ModelAttribute(&quot;acthasform&quot;) ActHasForm acthasform, @RequestParam(value = &quot;cers&quot;, required = false) int[] cers) {
        System.out.println(&quot;начинаем вывод&quot; + regulatoryform);
        regulatoryform.getFormFrequency().forEach(o-&gt;System.out.println(o));
        atiServiceImpl.saveRegulatoryForm(regulatoryform);
        acthasform.setFormId(regulatoryform);
        atiServiceImpl.saveActHasForm(acthasform);

        if (cers != null) {
            FormFrequency formFrequency = null;
            for (int i = 0; i &lt; cers.length; i++) {
                formFrequency = new FormFrequency();
                formFrequency.setId(cers[i]);
                regulatoryform.getFormFrequency().add(formFrequency);
            }
            for (int a = 0; a &lt; regulatoryform.getFormFrequency().size(); a++) {
                FormHasFrequency formHasFrequency = new FormHasFrequency();
                formHasFrequency.setFormId(regulatoryform);
                formHasFrequency.setFrequencyId(regulatoryform.getFormFrequency().get(a));
                atiServiceImpl.saveFormHasFrequency(formHasFrequency);
            }
        }

        return &quot;redirect:/regulatoryform/&quot;;
    }
英文:

I have solved my problem

my html 
&lt;th:block th:each=&quot;frequency : ${formfrequencylist}&quot;&gt;
&lt;input type=&quot;checkbox&quot;
name=&quot;cers&quot; 
th:value=&quot;${frequency.id}&quot; /&gt; 
&lt;label th:text=&quot;${frequency.frequencyNm}&quot;&gt;&lt;/label&gt;
&lt;/th:block&gt;
In my Entity i added 
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(name=&quot;form_has_frequency&quot;,joinColumns = @JoinColumn(name=&quot;form_id&quot;),inverseJoinColumns = @JoinColumn(name=&quot;frequency_id&quot;))
private List&lt;FormFrequency&gt; formFrequency=new ArrayList&lt;FormFrequency&gt;();

And my controller

	@PreAuthorize(&quot;hasRole(&#39;ROLE_ADMIN&#39;)&quot;)
@PostMapping(value = &quot;create/&quot;)
public String create(@ModelAttribute(&quot;regulatoryform&quot;) RegulatoryForm regulatoryform,@ModelAttribute(&quot;acthasform&quot;)ActHasForm acthasform,@RequestParam(value = &quot;cers&quot; , required = false) int[] cers ) {
System.out.println(&quot;начинаем вывод&quot;+  regulatoryform);
regulatoryform.getFormFrequency().forEach(o-&gt;System.out.println(o));
atiServiceImpl.saveRegulatoryForm(regulatoryform);
acthasform.setFormId(regulatoryform);
atiServiceImpl.saveActHasForm(acthasform);
if(cers != null) 
{ FormFrequency formFrequency = null ; 
for (int i = 0; i &lt; cers.length; i++) {
formFrequency = new FormFrequency();
formFrequency.setId(cers[i]);
regulatoryform.getFormFrequency().add(formFrequency);
}
for (int a = 0; a &lt; regulatoryform.getFormFrequency().size(); a++) {
FormHasFrequency formHasFrequency=new FormHasFrequency();
formHasFrequency.setFormId(regulatoryform);
formHasFrequency.setFrequencyId(regulatoryform.getFormFrequency().get(a));
atiServiceImpl.saveFormHasFrequency(formHasFrequency);
}
} 
return &quot;redirect:/regulatoryform/&quot;;
}

huangapple
  • 本文由 发表于 2023年5月29日 19:16:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定