使用SpringWeb+Thymeleaf和前后端分离两种技术完成同一个多层架构的web项目,项目内容为用户登录注册界面,登录成功后输出用户名和密码,前端技术选择Ajax,具体代码是什么
时间: 2023-06-16 21:08:27 浏览: 95
本系统采用前后端分离的设计架构,通过使用SpringBoot框架实现后端服务
1. 前后端分离
前端代码:
HTML:
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
</div>
<button type="button" id="loginBtn">登录</button>
<button type="button" id="registerBtn">注册</button>
</form>
<h1 id="result"></h1>
<script>
$(function () {
$("#loginBtn").click(function () {
var username=$("#username").val();
var password=$("#password").val();
$.ajax({
url: "/login",
type: "post",
data: {"username":username,"password":password},
success: function (result) {
$("#result").html(result);
},
error: function () {
$("#result").html("登录失败");
}
})
});
$("#registerBtn").click(function () {
window.location.href="/register";
});
});
</script>
</body>
</html>
```
JS:
```js
$(function () {
$("#loginBtn").click(function () {
var username=$("#username").val();
var password=$("#password").val();
$.ajax({
url: "/login",
type: "post",
data: {"username":username,"password":password},
success: function (result) {
$("#result").html(result);
},
error: function () {
$("#result").html("登录失败");
}
})
});
$("#registerBtn").click(function () {
window.location.href="/register";
});
});
```
后端代码:
Controller:
```java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
@GetMapping("/")
public String index() {
return "index";
}
@PostMapping("/login")
@ResponseBody
public String login(@RequestParam("username") String username,
@RequestParam("password") String password) {
//验证用户名和密码,这里只是模拟
if("admin".equals(username) && "123456".equals(password)) {
return "登录成功!用户名:" + username + ",密码:" + password;
} else {
return "用户名或密码错误!";
}
}
@GetMapping("/register")
public String register() {
return "register";
}
}
```
2. SpringWeb+Thymeleaf
前端代码:
HTML:
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="#" th:action="@{/login}" th:method="post">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
</div>
<button type="submit">登录</button>
<a th:href="@{/register}">注册</a>
</form>
<h1 th:text="${result}"></h1>
</body>
</html>
```
后端代码:
Controller:
```java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
@GetMapping("/")
public String index() {
return "index";
}
@PostMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model) {
//验证用户名和密码,这里只是模拟
if("admin".equals(username) && "123456".equals(password)) {
model.addAttribute("result", "登录成功!用户名:" + username + ",密码:" + password);
} else {
model.addAttribute("result", "用户名或密码错误!");
}
return "index";
}
@GetMapping("/register")
public String register() {
return "register";
}
}
```
阅读全文