随着传统文化复兴浪潮的兴起,汉服作为中华民族传统服饰的代表,正受到越来越多年轻人的喜爱和追捧。然而,高品质汉服往往价格不菲,且穿着场合有限,这使得许多潜在爱好者望而却步。针对这一市场痛点,我们设计并开发了"华服云裳"——一个基于JSP技术的汉服文化推广与租赁交易平台。
该系统采用经典的JSP+Servlet+JavaBean架构模式,严格遵循MVC设计原则。前端视图层使用JSP页面结合JSTL标签库实现数据动态渲染,避免了在页面中嵌入过多Java脚本代码,保证了代码的清晰度和可维护性。控制层由Servlet组件负责接收和分发用户请求,业务逻辑层通过JavaBean实现核心业务处理,数据持久层则基于JDBC技术与MySQL数据库进行交互。
数据库架构设计亮点
系统共设计20张数据表,涵盖了用户管理、商品展示、租赁订单、库存管理等核心业务模块。以下是几个关键表的设计分析:
汉服商品表(hanfu_goods)的设计体现了完善的商品管理体系:
CREATE TABLE hanfu_goods (
goods_id INT PRIMARY KEY AUTO_INCREMENT,
goods_name VARCHAR(100) NOT NULL,
dynasty VARCHAR(50) COMMENT '朝代:唐、宋、明等',
style VARCHAR(50) COMMENT '形制:曲裾、直裾、襦裙等',
material VARCHAR(100) COMMENT '面料材质',
size_range VARCHAR(50) COMMENT '尺码范围',
rental_price DECIMAL(10,2) NOT NULL COMMENT '租赁单价',
purchase_price DECIMAL(10,2) COMMENT '购买价格',
stock_quantity INT DEFAULT 0,
current_status ENUM('available','rented','maintenance') DEFAULT 'available',
description TEXT,
main_image VARCHAR(255),
detail_images JSON,
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_dynasty (dynasty),
INDEX idx_status (current_status),
INDEX idx_price (rental_price)
);
该表设计具有以下技术亮点:
- 使用ENUM类型严格约束商品状态,确保数据一致性
- JSON字段存储详情图片,适应多图展示需求
- 复合索引设计优化了按朝代、状态、价格等条件的查询效率
- 自动时间戳记录便于业务追踪和数据分析
租赁订单表(rental_orders)采用事务安全设计:
CREATE TABLE rental_orders (
order_id VARCHAR(32) PRIMARY KEY COMMENT '订单号',
user_id INT NOT NULL,
goods_id INT NOT NULL,
rental_days INT NOT NULL COMMENT '租赁天数',
start_date DATE NOT NULL,
end_date DATE NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
deposit_amount DECIMAL(10,2) COMMENT '押金',
order_status ENUM('pending','confirmed','shipped','completed','cancelled') DEFAULT 'pending',
payment_status ENUM('unpaid','paid','refunded') DEFAULT 'unpaid',
shipping_address TEXT,
contact_phone VARCHAR(20),
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
payment_time DATETIME,
complete_time DATETIME,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (goods_id) REFERENCES hanfu_goods(goods_id),
INDEX idx_user_status (user_id, order_status),
INDEX idx_create_time (create_time)
);
订单表通过外键约束确保数据完整性,状态字段的ENUM设计规范了业务流程,复合索引显著提升了用户订单查询和历史数据检索的性能。
核心业务功能实现
用户认证与权限管理
系统采用基于角色的访问控制(RBAC)模型,用户登录模块通过Servlet实现统一认证:
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String userType = request.getParameter("userType");
UserService userService = new UserService();
User user = userService.authenticate(username, password, userType);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("currentUser", user);
session.setAttribute("userType", userType);
// 根据用户类型跳转到不同页面
if ("admin".equals(userType)) {
response.sendRedirect("admin/dashboard.jsp");
} else {
response.sendRedirect("index.jsp");
}
} else {
request.setAttribute("errorMsg", "用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}

汉服商品展示与检索
商品展示模块支持多条件筛选和分页显示,前端通过AJAX实现无刷新数据加载:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="filter-section">
<select id="dynastyFilter" onchange="filterGoods()">
<option value="">所有朝代</option>
<option value="唐">唐代</option>
<option value="宋">宋代</option>
<option value="明">明代</option>
</select>
<select id="styleFilter" onchange="filterGoods()">
<option value="">所有形制</option>
<option value="曲裾">曲裾</option>
<option value="直裾">直裾</option>
<option value="襦裙">襦裙</option>
</select>
</div>
<div class="goods-grid">
<c:forEach items="${goodsList}" var="goods">
<div class="goods-item" data-dynasty="${goods.dynasty}" data-style="${goods.style}">
<img src="${goods.mainImage}" alt="${goods.goodsName}">
<h3>${goods.goodsName}</h3>
<p>朝代:${goods.dynasty} | 形制:${goods.style}</p>
<p class="price">¥${goods.rentalPrice}/天</p>
<button onclick="viewDetail(${goods.goodsId})">查看详情</button>
</div>
</c:forEach>
</div>
<script>
function filterGoods() {
const dynasty = document.getElementById('dynastyFilter').value;
const style = document.getElementById('styleFilter').value;
// AJAX请求筛选数据
fetch(`/goods/filter?dynasty=${dynasty}&style=${style}`)
.then(response => response.json())
.then(data => updateGoodsDisplay(data));
}
</script>

租赁订单处理流程
订单生成模块采用事务处理确保数据一致性,包含库存检查、价格计算、订单创建等步骤:
@Service
public class OrderService {
@Transactional
public OrderResult createRentalOrder(OrderRequest request) throws BusinessException {
// 1. 验证商品库存
Goods goods = goodsDAO.findById(request.getGoodsId());
if (goods.getStockQuantity() <= 0) {
throw new BusinessException("商品库存不足");
}
// 2. 计算租赁费用
BigDecimal rentalAmount = calculateRentalAmount(goods, request.getRentalDays());
BigDecimal deposit = calculateDeposit(goods);
BigDecimal totalAmount = rentalAmount.add(deposit);
// 3. 创建订单
Order order = new Order();
order.setOrderId(generateOrderId());
order.setUserId(request.getUserId());
order.setGoodsId(request.getGoodsId());
order.setRentalDays(request.getRentalDays());
order.setTotalAmount(totalAmount);
order.setDepositAmount(deposit);
order.setOrderStatus(OrderStatus.PENDING);
orderDAO.save(order);
// 4. 更新商品库存
goodsDAO.decreaseStock(request.getGoodsId());
return new OrderResult(order, rentalAmount, deposit);
}
private BigDecimal calculateRentalAmount(Goods goods, int days) {
return goods.getRentalPrice().multiply(BigDecimal.valueOf(days));
}
}

后台管理系统
管理员后台提供完整的业务管理功能,采用分层权限设计:
@WebServlet("/admin/*")
public class AdminDispatcherServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 权限验证
HttpSession session = request.getSession();
User user = (User) session.getAttribute("currentUser");
if (user == null || !"admin".equals(session.getAttribute("userType"))) {
response.sendRedirect("../admin-login.jsp");
return;
}
String path = request.getPathInfo();
switch (path) {
case "/goods":
handleGoodsManagement(request, response);
break;
case "/orders":
handleOrderManagement(request, response);
break;
case "/users":
handleUserManagement(request, response);
break;
default:
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
private void handleGoodsManagement(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
GoodsService service = new GoodsService();
if ("add".equals(action)) {
// 处理商品添加逻辑
Goods goods = extractGoodsFromRequest(request);
service.addGoods(goods);
response.sendRedirect("goods-management.jsp?msg=添加成功");
} else if ("update".equals(action)) {
// 处理商品更新逻辑
Goods goods = extractGoodsFromRequest(request);
service.updateGoods(goods);
response.sendRedirect("goods-management.jsp?msg=更新成功");
}
}
}

购物车与租赁管理
购物车模块采用Session存储临时数据,支持多商品同时租赁:
<%
// 购物车逻辑处理
List<CartItem> cart = (List<CartItem>) session.getAttribute("shoppingCart");
if (cart == null) {
cart = new ArrayList<>();
session.setAttribute("shoppingCart", cart);
}
// 添加商品到购物车
if ("addToCart".equals(request.getParameter("action"))) {
int goodsId = Integer.parseInt(request.getParameter("goodsId"));
int days = Integer.parseInt(request.getParameter("days"));
GoodsService goodsService = new GoodsService();
Goods goods = goodsService.getGoodsById(goodsId);
CartItem item = new CartItem(goods, days);
cart.add(item);
}
%>
<div class="cart-container">
<c:forEach items="${sessionScope.shoppingCart}" var="item" varStatus="status">
<div class="cart-item">
<img src="${item.goods.mainImage}" width="80">
<div class="item-info">
<h4>${item.goods.goodsName}</h4>
<p>租赁天数: ${item.days}天</p>
<p>小计: ¥<fmt:formatNumber value="${item.goods.rentalPrice * item.days}"
pattern="#,##0.00"/></p>
</div>
<button onclick="removeFromCart(${status.index})">删除</button>
</div>
</c:forEach>
</div>

实体模型与业务逻辑
系统核心实体模型采用面向对象设计,每个实体都对应相应的数据表:
public class HanfuGoods {
private Integer goodsId;
private String goodsName;
private String dynasty;
private String style;
private String material;
private String sizeRange;
private BigDecimal rentalPrice;
private BigDecimal purchasePrice;
private Integer stockQuantity;
private GoodsStatus currentStatus;
private String description;
private String mainImage;
private List<String> detailImages;
private Date createTime;
private Date updateTime;
// 业务方法
public boolean isAvailable() {
return GoodsStatus.AVAILABLE.equals(currentStatus) && stockQuantity > 0;
}
public BigDecimal calculateRental(int days) {
return rentalPrice.multiply(new BigDecimal(days));
}
}
public class RentalOrder {
private String orderId;
private Integer userId;
private Integer goodsId;
private Integer rentalDays;
private Date startDate;
private Date endDate;
private BigDecimal totalAmount;
private BigDecimal depositAmount;
private OrderStatus orderStatus;
private PaymentStatus paymentStatus;
private String shippingAddress;
private String contactPhone;
private Date createTime;
private Date paymentTime;
private Date completeTime;
// 订单状态机转换
public boolean canBeCancelled() {
return orderStatus == OrderStatus.PENDING || orderStatus == OrderStatus.CONFIRMED;
}
public void confirmOrder() {
if (orderStatus != OrderStatus.PENDING) {
throw new IllegalStateException("只有待确认订单可以确认");
}
this.orderStatus = OrderStatus.CONFIRMED;
}
}
系统优化与功能扩展方向
智能化推荐系统
- 实现基于用户浏览历史和偏好的汉服推荐算法
- 采用协同过滤和内容推荐相结合的技术方案
- 集成机器学习模型进行个性化推荐
移动端适配与小程序开发
- 开发响应式前端设计,适配移动设备
- 基于微信小程序框架开发轻量级应用
- 实现扫码租赁、位置服务等移动特色功能
供应链管理系统集成
- 扩展供应商管理模块,实现汉服采购、质检流程数字化
- 开发清洁维护跟踪系统,确保服饰卫生标准
- 集成物流跟踪接口,提供完整的租赁物流解决方案
社交化功能增强
- 添加用户晒单、穿搭分享社区功能
- 实现汉服爱好者社交网络,促进用户互动
- 开发活动预约系统,支持汉服文化活动组织
数据分析与商业智能
- 构建数据仓库,进行租赁业务数据分析
- 开发销量预测模型,优化库存管理
- 实现用户行为分析,指导营销策略制定
该系统通过严谨的技术架构设计和完善的业务功能实现,为汉服文化的传播和商业推广提供了有力的数字化支撑。未来通过持续的功能优化和技术升级,将进一步增强平台的服务能力和用户体验。