重定向redirect

springboot重定向redirect

最近在做学生表在页面的增删改查的时候,在没有使用ajax的情况下,在在增改的时候需要跳转到另外的页面进行修改,然后返回学生表页面,进行动态刷新。那么我们需要进行重定向,在这里我们选择redirect。但是在实际操作过程中,我还是出了一些问题,这些问题现在想来是自己没有理解透彻。

如下是示例

控制器

  @GetMapping("/student")
    public String student(Model model){
        List<Student> students=stuService.getAll();
        model.addAttribute("stu",students);
        return "students";//返回学生页面
    }
 @PostMapping("/update")
    public String receiveStudent(Student student){
        int flag = stuService.updateStu(student);
        return "redirect:/student";
    }

html代码,这里是进行修改页面

 <form class="form-horizontal" style="margin-top: 40px" th:action="@{/update}" th:method="post">
                <div class="form-group">
                    <label for="cno" class="col-sm-2 control-label">学号</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="cno" name="cno" th:value="${stu.cno}" placeholder="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">姓名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" th:value="${stu.name}" placeholder="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="age" class="col-sm-2 control-label">年龄</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="age" name="age" th:value="${stu.age}" placeholder="">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default bg-info">修改学生</button>
                    </div>
                </div>
            </form>

我之前在调试的时候,发现点击修改学生或者添加学生后,确实可以实现在数据库中进行了相应操作,但是每次redirect返回都是404,我开始还想,redirect:/students没写错呀。那好吧我就说说我哪里错误了,这里不能写students,这是html的名字。因为redirect后面更的是映射,是映射啊!!怪不得会返回404,因此因为写student。

总结

redirect:/映射

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程初学者01

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值