【问题标题】:How to get checked checkbox value from html page to spring mvc controller如何从html页面获取选中的复选框值到spring mvc控制器
【发布时间】:2013-05-29 07:00:03
【问题描述】:

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

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> 

*控制器*

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

【问题讨论】:

  • 嗯,很好。但是,如果您不添加向我们展示如何您已经尝试过的代码,我们将无法帮助您。您必须提出具体问题才能获得有用的答案。
  • 我刚刚更新了我的问题。可以吗???

标签: spring thymeleaf


【解决方案1】:

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

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

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

【讨论】:

  • thnx bt 它不是 groovy ..它是 scala
  • 啊。哎呀。我需要多出去走走。 :)
【解决方案2】:

我已经将 JSTL 与 JSP 一起使用,而 thymeleaf 是新事物。我阅读了THYMELEAF 文档。

有一个部分解释了多值复选框。

<input type="checkbox" 
     class="ads_Checkbox" 
     th:text="${q.questionName}" 
     th:value="${q.id}" name="id"/>

在上面的代码中,我们没有将值绑定到命令对象的字段。而是尝试这样做

<input type="checkbox" 
     class="ads_Checkbox" 
     th:text="${q.questionName}" 
     th:field="*{selectedQuestions}" 
     th:value="${q.id}" />

这里的selectedQuestions是一个存在于spring命令对象中的数组对象。

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多