AbuCoder 2023-02-28 18:48 采纳率: 50%
浏览 168
已结题

SpringBoot表单保存

如何把这个表单提交到后端,存储到数据库里,给改下,代码,谢谢!

img

下面是我的写法,但是我搞不定。只有把这个数据一起存储到数据库即可!!

<form class="form-horizontal m" id="form-overallevaluation-add">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h3>综合素质自评</h3>
                    </div>
                    <div class="ibox-content">
                        <table class="table">
                            <thead>
                            <tr>
                                <th scope="col" class="text-center" style="font-weight: bold;font-size: 18px;width: 100px;"><div class="alert alert-success" style="margin-bottom: 5px;">序号 </div></th>
                                <th scope="col" class="text-center" style="font-weight: bold;font-size: 18px;"><div class="alert alert-success " style="margin-bottom: 5px;">评价内容</div></th>
                                <th scope="col" class="text-center" style="font-weight: bold;font-size: 18px;width: 130px;"><div class="alert alert-success" style="margin-bottom: 5px;">学生自评</div></th>
                            </tr>
                            </thead>
                            <tr th:each="qitem,item:${qlist}">
                                <td class="text-center" scope="row"><div class="tdstyle alert  alert-info"><span th:text="${item.index+1}"></span></div></td>
                                <td><div class="tdstyle alert  alert-info "><span th:id="'question_'+${qitem.itemid}" th:text="${qitem.itemTitle}"></span></div></td>
                                <td><div class="tdstyle alert  alert-info">


                                    <select th:id="'question_'+${qitem.itemid}+'_option'"  name="selfAssessment" class="form-control m-b" th:with="type=${@dict.getType('system_rank')}">
                                        <option  th:attr="data-id=${qitem.itemid}" th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                    </select>
                                </div></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        
<a class="btn btn-primary btn-rounded btn-block" href="javascript:void(0)" onclick="submitData()"><i class="fa fa-info-circle"></i> 提交</a> </form> let obj = $('#form-overallevaluation-add').serialize(); console.log(obj) $.post(prefix + "/submitOptions", obj, function(result) { console.log(result) if (result.code == web_status.SUCCESS) { $.modal.msgSuccess("提交成功") $.modal.close(); } else { $.modal.msgError(result.msg); } });
@PostMapping("/submitOptions")
    @ResponseBody
    public AjaxResult submitOptions(@RequestParam String semester,@RequestParam String options){
        System.out.println(semester);
        System.out.println(options);
        String[] selectoptions = options.split("&");
        for(int i = 0 ;i <= selectoptions.length -1;i++){
            String[] test = selectoptions[i].split("=");
            String qidstr = test[1].replace("\"", "");
            System.out.println(qidstr);
        }
        return AjaxResult.success();
    }

  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-02-28 18:51
    关注

    基于Monster 组和GPT的调写:
    在前端将表单数据序列化成符合后端接口要求的参数格式,并使用 AJAX 发送 POST 请求到后端。

    以下是可能的修改代码示例:

    function submitData() {
        var formData = $('#form-overallevaluation-add').serialize();
        var semester = '2022-01'; // 替换为您要提交的学期信息
        $.ajax({
            type: "POST",
            url: prefix + "/submitOptions",
            data: {
                semester: semester,
                options: formData
            },
            success: function(result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("提交成功");
                    $.modal.close();
                } else {
                    $.modal.msgError(result.msg);
                }
            }
        });
    }
    
    
    

    修改后的代码将 serialize() 方法返回的字符串作为参数 options 提交到后端,并且手动设置了学期参数 semester。后端接口的参数也做了相应的修改。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月9日
  • 已采纳回答 3月5日
  • 创建了问题 2月28日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改