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:/映射