JavaWeb [Jsp, el, jstl, comprehensive case] display contact list

Reprinted: Link to this article: http://blog.csdn.net/u013621398/article/details/108563928

Contact list operation effect

Insert picture description here

demand analysis

The essence is to query a List collection, and then forward the request to the page, and use the el expression to add a jstl tag to display it.

Insert picture description here

Write business logic

We are test-driven development, so we first write the test logic, then implement the logic and add the page
test\java\com\wzx\service\TestContactService.java

public class TestContactService {
    
    
    @Test
    public void test01(){
    
    
            //1:创建业务类对象
            ContactService contactService = new ContactService();
            //2:调用查询
            List<Contact> contactList = contactService.queryAll();
            //3:遍历
            for(Contact contact:contactList){
    
    
                System.out.println(contact);
            }
    }
}

Writing entity classes

The
essence of src\com\wzx\bean\Contact.java is to model the data

//联系人实体类
public class Contact {
    
    
    private int id;
    private String name;
    private String sex;
    private int age;
    private String address;
    private String qq;
    private String email;

Write business class

src\com\wzx\service\ContactService.java

//service层
public class ContactService {
    
    
    //1:创建数据层访问对象
    private ContactDao dao = new ContactDao();
    public List<Contact> queryAll() {
    
    
        //2:调用全查方法
        return dao.findAll();
    }
}

Write the data access layer

src\com\wzx\dao\ContactDao.java
currently does not integrate jdbc and mysql, because this section focuses on the three-tier architecture, as long as you highlight the three-tier

//数据访问层
public class ContactDao {
    
    
    public List<Contact> findAll() {
    
    
        //创建集合
        List<Contact> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
    
    
            //添加元素
            Contact contact = new Contact(i,"name"+i,"男",10,"bj","QQ","[email protected]");
            list.add(contact);
        }
        return list;
    }
}

At this point, you can run a test test, as long as there is no problem with the data, it is relatively easy to increase the page display

Add page servlet

src\com\wzx\web\ContactListServlet.java

@WebServlet("/list")
public class ContactListServlet extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
            doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //1:创建业务类对象
        ContactService contactService = new ContactService();
        //2:调用查询
        List<Contact> contactList = contactService.queryAll();
        //3:遍历打印,改成请求转发,并且使用el与jstl来展示
        request.setAttribute("list",contactList);
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
}

Add page el and jstl

Add the jar of jstl

Insert picture description here

Create a new page to import tags and traverse data

web\list.jsp

<%@page import="com.wzx.bean.Contact" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>Title</title>
</head>
<body>

        <table border="1" >
            <tr >
                <th>编号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>籍贯</th>
                <th>QQ</th>
                <th>邮箱</th>
                <th>操作</th>
            </tr>

            <c:forEach items="${list}" var="item" >
                <tr >
                    <td>${
    
    item.id}</td>
                    <td>${
    
    item.name}</td>
                    <td>${
    
    item.sex}</td>
                    <td>${
    
    item.age}</td>
                    <td>${
    
    item.address}</td>
                    <td>${
    
    item.qq}</td>
                    <td>${
    
    item.email}</td>
                    <td>删除</td>
                </tr>
            </c:forEach>
        </table>
</body>
</html>

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108599572