架构探险-第二章:为Web应用添加业务功能(9)-完善视图层

一,前言

上篇以CustomerServlet为例,对控制器层进行完善
通过依赖CustomerService,获取客户数据,放入请求属性,并重定向到customer.jsp
这篇同样以customer.jsp为例,对齐进行完善,获取并展示数据

二,完善视图层

customer.jsp:

<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%--获取上下文--%>
<c:set var="BASE" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <title>客户管理</title>
</head>
<body>

<h1>客户列表</h1>

<table>
    <tr>
        <th>客户名称</th>
        <th>联系人</th>
        <th>电话号码</th>
        <th>邮箱地址</th>
        <th>操作</th>
    </tr>

    <%--循环customerList进行赋值--%>
    <c:forEach var="customer" items="${customerList}">
        <tr>
            <td>${customer.name}</td>
            <td>${customer.contact}</td>
            <td>${customer.telephone}</td>
            <td>${customer.email}</td>
            <td>
                <a href="${BASE}/customer_edit?id=${customer.id}">编辑</a>
                <a href="${BASE}/customer_delete?id=${customer.id}">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>

</body>
</html>

三,测试服务

添加web配置:
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
</web-app>

运行服务:


三,结尾

有了优化后的服务层,控制器层和视图层的完善格外简单
至此,整套CustomerServlet开发完毕了,但还有另外4个Service等待着我们开发

猜你喜欢

转载自blog.csdn.net/abap_brave/article/details/80526902
今日推荐