gpt4 book ai didi

spring - 如何从 html 页面获取选中的复选框值到 spring mvc Controller

转载 作者:行者123 更新时间:2023-12-01 10:00:31 24 4
gpt4 key购买 nike

我正在使用带有 thymeleaf 模板引擎的 spring mvc 框架问题是,我有 1 个页面有多个复选框迭代 thymeleaf th:each 迭代器。当我单击多个复选框时,我想将复选框值传递给 Controller ​​方法..

html内容

<table> 
<tr th:each="q : ${questions}">
<h3 th:text="${q.questionPattern.questionPattern}"></h3>
<div>
<p >
<input type="checkbox" class="ads_Checkbox" th:text="${q.questionName}" th:value="${q.id}" name="id"/>
</p>
</div>
</tr>
</table>

* Controller *

 @RequestMapping(value = Array("/saveAssessment"), params = Array({ "save" }))
def save(@RequestParam set: String, id:Long): String = {
var userAccount: UserAccount = secService.getLoggedUserAccount
println(userAccount)
var questionSetQuestion:QuestionSetQuestion=new QuestionSetQuestion
var questionSet: QuestionSet = new QuestionSet
questionSet.setUser(userAccount)
questionSet.setSetName(set)
questionSet.setCreatedDate(new java.sql.Date(new java.util.Date().getTime))
questionSetService.addQuestionSet(questionSet)
var list2: List[Question] = questionService.findAllQuestion
var limit=list2.size
var qustn:Question=null
var a = 1;
for( a <- 1 to limit ){
println( a );
qustn= questionService.findQuestionById(a)
questionSetQuestion.setQuestion(qustn)
questionSetQuestion.setQuestionSet(questionSet)
questionSetQuestion.setCreatedDate(new java.sql.Date(new java.util.Date().getTime))

questionSetQuestionService.addQuestionSetQuestion(questionSetQuestion) } "redirect:/teacher/Assessment.html" }

最佳答案

我想你几乎已经做到了。使用复选框,您只能使用表单发回一条信息……这就是值。因此,如果您试图确定当用户单击提交按钮时哪些复选框被选中,那么我会让所有复选框都使用一个名称...例如“id”(就像您拥有的一样)。值是问题的实际 id(再次像你一样)。提交后,“id”将是一个字符串数组,其中包含已选中复选框的所有值。

所以你的 Controller 方法需要将名为“ids”的参数映射到参数“id”,它是一个字符串[]。现在对于每个 id,您可以调用 questionService.findQuestionById。

(我不是 Groovy 大师,所以没有代码示例 sry :)

关于spring - 如何从 html 页面获取选中的复选框值到 spring mvc Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com