11.18

完成顾客首页的语法优化后,下一步可聚焦于完善房产详情页与交易流程,并逐步实现其他角色(经纪人、管理员)的核心功能。以下是具体步骤:

第一步:开发房产详情页(顾客端)

1. 创建详情页模板(templates/customer/house-detail.html

展示房产的完整信息(编号、户型、面积、地址、状态等):

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>房产详情</title>
</head>
<body>
    <h2>房产详情</h2>
    <div th:if="${house}">
        <p>编号:<span th:text="${house.houseId}"></span></p>
        <p>户型:<span th:text="${house.roomType}"></span></p>
        <p>面积:<span th:text="${house.area}"></span> ㎡</p>
        <p>地址:<span th:text="${house.address}"></span></p>
        <p>售价:<span th:text="${house.price}"></span> 元</p>
        <p>状态:<span th:text="${house.status}"></span></p>
        <a th:href="@{/customer/house/buy(houseId=${house.houseId})}">立即购买</a>
    </div>
    <div th:unless="${house}">
        房产不存在或已售罄!
    </div>
</body>
</html>

2. 实现详情页后端逻辑(CustomerController补充)

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private HouseService houseService;
    @Autowired
    private HouseRepository houseRepository;

    // 房产详情页
    @GetMapping("/house/detail")
    public String houseDetail(@RequestParam String houseId, Model model) {
        House house = houseRepository.findById(houseId).orElse(null);
        model.addAttribute("house", house);
        return "customer/house-detail";
    }
}

第二步:完善交易流程(顾客→经纪人→管理员)

1. 顾客下单后,经纪人确认交易

  • 经纪人首页:创建templates/agent/index.html,展示负责的“意向”状态房产:

    <table border="1">
        <tr>
            <th>地址</th>
            <th>顾客</th>
            <th>操作</th>
        </tr>
        <tr th:each="house : ${houses}">
            <td th:text="${house.address}"></td>
            <td th:text="${house.userId}"></td>
            <td>
                <a th:href="@{/agent/house/confirm(houseId=${house.houseId})}">确认交易</a>
            </td>
        </tr>
    </table>
    
  • 经纪人控制器AgentController.java,处理交易确认:

    @Controller
    @RequestMapping("/agent")
    public class AgentController {
        @Autowired
        private HouseService houseService;
        @Autowired
        private AgentRepository agentRepository;
    
        // 经纪人首页(查看负责的“意向”房产)
        @GetMapping("/index")
        public String index(Model model, HttpSession session) {
            String agentId = (String) session.getAttribute("agentId");
            List<House> houses = houseService.findByStatusAndAgentId("意向", agentId);
            model.addAttribute("houses", houses);
            return "agent/index";
        }
    
        // 确认交易(状态改为“售出”)
        @GetMapping("/house/confirm")
        public String confirmTransaction(@RequestParam String houseId) {
            houseService.updateStatus(houseId, "售出");
            return "redirect:/agent/index";
        }
    }
    

2. 管理员停售/删除房产

  • 管理员停售页:创建templates/admin/house-stop.html,展示“在售”房产列表:

    <table border="1">
        <tr>
            <th>地址</th>
            <th>操作</th>
        </tr>
        <tr th:each="house : ${houses}">
            <td th:text="${house.address}"></td>
            <td>
                <a th:href="@{/admin/house/stop(houseId=${house.houseId})}">停售</a>
            </td>
        </tr>
    </table>
    
  • 管理员控制器AdminController补充停售逻辑:

    @Controller
    @RequestMapping("/admin")
    public class AdminController {
        @Autowired
        private HouseService houseService;
    
        // 停售房产(状态改为“停售”)
        @GetMapping("/house/stop")
        public String stopHouse(@RequestParam String houseId) {
            houseService.updateStatus(houseId, "停售");
            return "redirect:/admin/house-list";
        }
    }
    

第三步:测试全流程

  1. 顾客流程:登录→浏览房产→查看详情→下单(状态变“意向”)。
  2. 经纪人流程:登录→确认交易(状态变“售出”)。
  3. 管理员流程:登录→停售房产(状态变“停售”)。

后续开发方向

  1. 添加密码加密(使用Spring Security的BCrypt),提升安全性。
  2. 优化前端样式(引入Bootstrap/CSS),使页面更美观。
  3. 完善异常处理(如房产已售、权限不足时的提示)。

按此步骤推进,可实现系统的核心业务闭环,确保各角色功能完整。

posted @ 2025-11-23 12:48  muyuxiaxing  阅读(3)  评论(0)    收藏  举报