Submit按钮无法重定向至/students页面问题排查求助
问题分析与修复方案
核心问题1:表单元素未被form标签包裹
你的<form>标签直接提前闭合,所有输入框和提交按钮都处于form标签外部,导致点击Submit时,浏览器无法识别这是表单提交操作,自然不会触发POST请求。必须将所有表单相关元素(输入框、提交按钮)嵌套在form标签内部。
核心问题2:Thymeleaf字段绑定语法错误
当通过th:object="${student}"绑定Student对象后,th:field应直接引用对象属性(如*{firstName}),而非*{student.firstName}——th:object已指定当前上下文对象为student,无需重复添加对象前缀。
修正后的完整代码
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Student Management System</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-6 col-md-6 col-sm-6 container justify-content-center card"> <h1 class="text-center">Create New Student</h1> <div class="card-body"></div> <!-- 表单标签包裹所有表单元素 --> <form th:action="@{/students}" th:object="${student}" method="POST"> <div class="form-group"> <label>Student First Name</label> <input type="text" name="firstName" th:field="*{firstName}" class="form-control" placeholder="Enter Student First Name" /> </div> <div class="form-group"> <label>Student Last Name</label> <input type="text" name="lastName" th:field="*{lastName}" class="form-control" placeholder="Enter Student Last Name" /> </div> <div class="form-group"> <label>Student Email</label> <input type="text" name="email" th:field="*{email}" class="form-control" placeholder="Enter Student Email" /> </div> <div class="box-footer"> <button class="btn btn-primary" type="submit" value="submit">Submit</button> </div> </form> </div> </div> </div> </body> </html>
内容的提问来源于stack exchange,提问作者Cuneyt YAZ
相关产品推荐
相关产品推荐

