最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
如何理解Java后端三层架构?MVC模式与三层架构有何区别?
时间:2026-07-24 09:15:48 编辑:袖梨 来源:一聚教程网
一、Spring MVC 综合案例
1. 案例一:加法计算器
接口定义
- 功能:两数相加。
- 路径:calc/sum
- 参数:
| 参数名称 | 类型 | 是否必须 |
|---|---|---|
| num1 | Integer | 是 |
| num2 | Integer | 是 |
响应数据:
- contentType:text/html
- 响应内容:计算结果:ans
代码实现
- 前端静态页面
calc.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>加法计算器</title>
</head>
<body>
<h1>简易计算器</h1>
<!-- 表单提交地址 /calc/sum,请求方式post -->
<form action="calc/sum" method="post">
数字1:<input type="text" name="num1">
数字2:<input type="text" name="num2">
<input type="submit" value="点击相加">
</form>
</body>
</html>
访问地址:http://127.0.0.1:8080/calc.html
- 后端接口
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/calc")
public class CalcController {
@RequestMapping("/sum")
public String sum(Integer num1, Integer num2){
Integer total = num1 + num2;
return "<h1>计算结果:" + total + "</h1>";
}
}
2. 案例二:用户登录系统
接口定义
1. 检验用户信息
- 功能:检验账号密码是否正确。
- 路径:/user/login
- 请求方式:POST
- 参数:
| 参数名称 | 类型 | 是否必须 |
|---|---|---|
| username | String | 是 |
| password | String | 是 |
响应数据:
- contentType:text/html
响应内容:
- true //验证成功
- false //验证失败
2. 查询登录用户
- 功能:查询当前登录的用户
- 请求路径:/user/getLoginUser
- 请求方式:GET
- 请求参数:无
响应数据:
- contentType:text/html
- 响应内容:admin
后端
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class LoginController {
// 登录校验接口
@RequestMapping("/login")
public boolean login(String userName, String password, HttpSession session){
// 判断账号密码是否为空
if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
return false;
}
// 模拟数据库账号密码
if("zhangsan".equals(userName) && "123456".equals(password)){
// 登录成功,存入session
session.setAttribute("loginUser", userName);
return true;
}
return false;
}
// 获取当前登录用户
@RequestMapping("/getLoginUser")
public String getLoginUser(HttpSession session){
// getSession(false):无会话不创建新session
String user = (String) session.getAttribute("loginUser");
return user == null ? "" : user;
}
}
注意:
- 代码要尽量减少嵌套,增加可读性
- 判断两个字符串是否相等的时候要把常量写在前面,变量写在equal()里,防止空指针异常:
"admin".equals(username)
前端
采用AJAX
- AJAX ( Asynchronous JavaScript And XML) 异步 JavaScript 和 XML
- 在不重新加载整个页面的情况下,与服务器交换数据、局部更新网页内容。
login.html
<body>
<h1>用户登录</h1>
用户名:<input name="username" type="text" id="username">
密码:<input name="password" type="password" id="password">
<input type="button" value="登录" onclick="login()">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script> function login() {
$.ajax({
type: "post",
url: "user/login",
data: {
username: $("#username").val(),
password: $("#password").val()
},
//http请求成功
success: function(result){
if(result){
//密码正确,跳转
location.href = "index.html";
}else{
//密码错误
alert("账户或密码错误");
} }
});
}
</script>
</body>
index.html
<body>
登录人: <p id="loginUser"></p>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script> $.ajax({
type:"get",
url: "/user/getLoginUser",
success:function (username){
$("#loginUser").text(username);
}
})
</script>
</body>
3. 案例三:留言板
Lombok
引入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
常用注解
Lombok主要作用是自动生成get、set、toString等方法,生命周期只在源码阶段。
| 注解 | 作用 |
|---|---|
| @Data | 综合注解:自动生成getter、setter、toString、equals、hashCode、无参构造 |
| @Getter/@Setter | 单独生成get/set方法 |
| @NoArgsConstructor | 无参构造方法 |
| @AllArgsConstructor | 全参构造方法 |
| @NonNull | 属性非空校验 |
注意:
@Data=@Getter+@Setter+toString()+equals()+hashCode()@Data生成的内容非常多,可以使用下面的注解生成具体的函数@Getter/@Setter既是类注解,也是属性注解。放在类上表示对所有属性生成getter和setter方法;放在属性上,表示单独对这个属性生成getter和setter

接口设计
1. 获取全部留言
- 请求方式:GET
- 请求路径:/message/getList
- 响应数据:
Content-Type: application/json- 类型:JSON
2. 发送留言
- 请求方式:POST
- 请求路径:/message/publish
- 响应数据:JSON格式
- 相应内容:
- ok:0 //发布失败
- ok:1 //发布成功
- 相应内容:
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
您可能感兴趣的文章:- Java Web实战之使用三层架构与Servlet构建登录注册模块
- java中MVC模式与三层架构
- java开发MVC三层架构上再加一层Manager层原理详解