springboot登录注册demo(三)--前后端传递
前端页⾯通过thymeleaf渲染
    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
前后端的传递关键在html上⾯,请看代码:
<!DOCTYPE html>
<html xmlns:th="">
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
<link rel="stylesheet" href="cdn.bootcss/bootstrap/3.3.4/css/bootstrap.min.css"/>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-3 column">
<form role="form" method="POST" th:action="@{/userLogin}" th:object="${user}">
<label for="username">Name</label><input type="text" class="form-control" id="username" th:field="*{name}"/>
<label for="password">Password</label><input type="password" class="form-control" id="password" th:field="*{password}"/> <button type="submit" class="btn btn-default">Sign in</button>
</form>thyme
<ul class="nav nav-pills">
<li role="presentation"><a href="register.html" class="href" target="_blank">Sign up</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
th:action="@{/userLogin}" 表⽰这个form表单的action会指向/userLogin
th:object="${user}" 表⽰form表单的内容会以user的形式传递
th:field:"*{name}" 表⽰该input输⼊的值,也就是前端的值存储在name中
如果你在前端输⼊name=jwen,password=1234,当这个表单提交的时候,就会把name=jwen,password=1234存放在user中传递给/userLogin
那么看看controller层怎么接接收这个的
@RequestMapping(value = "/userLogin", method = RequestMethod.POST)
String userLogin(User user, Model model) {
boolean verify = userService.verifyUser(user);
if (verify) {
model.addAttribute("name", Name());
model.addAttribute("password", Password());
return "result";
} else {
return "redirect:/notVerify";
}
}
requestMapping将/userLogin绑定给userLogin⽅法,该⽅法的⼊参是⼀个User的实例,⼀个Model的实例
⽽这个User的实例,就是我们从前端传递的,就是说你在userLogin⽅法,可以得到前端传递的东西;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。