SSM框架PUT请求前端报400,415,500

问题Spring MVC设置@PutMapping前端该如何发送请求 ?

例子:

    @ResponseBody
    @PutMapping("/update/{studentUsername}")
    public Msg updateStudent(Student student) String studentUsername){
        studentService.updateStudent(student);
        return Msg.success();
    }

前端请求:

委屈求全的解决方法:


    @ResponseBody
    @RequestMapping(value="/update/{studentUsername}",method = RequestMethod.POST)
    public Msg updateStudent(Student student,@PathVariable("studentUsername") String studentUsername){
        student.setStudentUsername(studentUsername);
        studentService.updateStudent(student);
        return Msg.success();
    }

前端请求:

reqwest({
              url: 'http://localhost:3000/student/update/'+this.props.studentUsername,
               method: 'post'
              ,type: 'json'
              ,data: this.props.form.getFieldsValue()
              ,error: function (err) {
                openNotificationWithIconError('error');
               }
              , success: function (data) {
                openNotificationWithIcon('success');
                console.log(data)
                }
            }).then((data) => {
                this.props.fetch();
            });
好的,批量删除可以通过如下步骤实现: 1. 在 Controller 中添加批量删除的请求处理方法,可以使用数组或者集合等数据结构来接收要删除的数据的 ID。 ```java @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST) @ResponseBody public Map<String, Object> deleteBatch(@RequestParam("ids[]") List<Integer> ids) { Map<String, Object> result = new HashMap<>(); try { // 调用 Service 层的批量删除方法 userService.deleteBatch(ids); result.put("success", true); } catch (Exception e) { result.put("success", false); result.put("message", e.getMessage()); } return result; } ``` 2. 在 Service 中实现批量删除的方法,可以使用 MyBatis 的批量删除功能。 ```java @Override public void deleteBatch(List<Integer> ids) { userMapper.deleteBatch(ids); } ``` 3. 在 Mapper 中添加批量删除的 SQL 语句。 ```xml <delete id="deleteBatch"> delete from user where id in <foreach collection="list" item="id" open="(" separator="," close=")"> #{id} </foreach> </delete> ``` 对于前端页面,可以使用 HTML、CSS 和 JavaScript 技术实现,下面是一个简单的示例: ```html <!-- 删除按钮 --> <button id="deleteBtn">删除</button> <!-- 用户列表 --> <table id="userTable"> <thead> <tr> <th><input type="checkbox" id="selectAll"></th> <th>ID</th> <th>用户名</th> <th>性别</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox" class="selectOne" data-id="1"></td> <td>1</td> <td>Tom</td> <td>男</td> <td><a href="#">编辑</a></td> </tr> <tr> <td><input type="checkbox" class="selectOne" data-id="2"></td> <td>2</td> <td>Mary</td> <td>女</td> <td><a href="#">编辑</a></td> </tr> <!-- ... --> </tbody> </table> <script> $(function() { // 全选/取消全选 $('#selectAll').click(function() { $('.selectOne').prop('checked', $(this).prop('checked')); }); // 批量删除 $('#deleteBtn').click(function() { var ids = []; $('.selectOne:checked').each(function() { ids.push($(this).data('id')); }); if (ids.length === 0) { alert('请选择要删除的数据!'); return; } if (confirm('确定要删除选中的数据吗?')) { $.ajax({ url: '/user/deleteBatch', type: 'post', data: {ids: ids}, success: function(result) { if (result.success) { alert('删除成功!'); location.reload(); } else { alert(result.message); } }, error: function() { alert('服务器错误!'); } }); } }); }); </script> ``` 以上代码仅供参考,实际开发中需要根据具体需求进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dying 搁浅

两杯酒,一杯敬你余生多欢喜。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值