搭建Spring MVC环境进行简单的增、删、改、查(二)

前台展示的Jsp页面如下:
Index.jsp:
%@page import="java.util.List"%
<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding="UTF-8"%>

 

%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %

Insert title here

    <form action="query.action" method="POST">

    <table>

        <tr>
            <td>Name: </td>  
            <td>
                <input type="text" name="name" />
            </td>
        </tr>

        <tr> 
            <td>Address: </td>
            <td>
                <input type="text" name="address" />
            </td>
        </tr>

        <tr>
            <td>Phone: </td>
            <td>
                <input type="text" name="phone"/>
            </td>
        </tr>

        <tr>
            <td><input type="submit" value="查询全部"/></td>
            <td><a href="${baseUrl}/toAddCustomer.action">Create New Customer</a></td>
        </tr>

    </table>

</form>

<br><br>

<c:if test="${!empty requestScope.customers }">

    <table border="1" cellpadding="10" cellspacing="0">

        <tr>
            <td>Name:</td>
            <td>Address:</td>
            <td>Phone:</td>
            <td>Update</td>
            <td>Delete</td>
        </tr>

        <c:forEach items="${requestScope.customers }" var="cust">

            <tr>
                <td>${cust.name }</td>
                <td>${cust.address }</td>
                <td>${cust.phone }</td>
                <td><a href="${baseUrl}/getCustomer.action?id=${cust.id}">UPDATE</a></td>
                <td><a href="${baseUrl}/delete.action?id=${cust.id}">DELETE</a></td>
            </tr>

        </c:forEach>    

    </table>
</c:if>

<!--  
    已经执行查询, 但返回的集合中没有任何元素时, 显示: 没有符合条件的员工信息. 
-->
<c:if test="${requestScope.customers != null }">
    <c:if test="${empty requestScope.customers }">
        没有符合条件的员工信息. 
    </c:if>
</c:if>



addCustomer.jsp:
<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding="UTF-8"%>
%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %

 

Insert title here

<br><br>

<form:form action="saveCustomer.action" method="post" 
    modelAttribute="customer">

    <table>

        <tr>
            <td>Name: </td>  
            <td>
                <form:input path="name"/>
            </td>
        </tr>

        <tr>
            <td>Address: </td>
            <td>
                <form:input path="address"/>
            </td>
        </tr>

        <tr>
            <td>Phone: </td>
            <td>
                <form:input path="phone"/>
            </td>
        </tr>

        <tr>
            <td colspan="2">  
                <input type="submit" value="Submit"/>
            </td>
        </tr>

    </table>

</form:form>



updateCustomer.jsp:
%@page import="java.util.List"%
<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding="UTF-8"%>
%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %
%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %

 

 

Insert title here

    <c:set value="${requestScope.customer}" var="cust"></c:set>

<form:form action="update.action" method="POST" modelAttribute="customer">
    <input type="hidden" name="id" value="${cust.id}">
    Name: <input type="text" name="name" value="${cust.name }" /><br>
    Address: <input type="text" name="address" value="${cust.address }"/><br>
    Phone:<input type="text" name="phone" value="${cust.phone }"/><br>
    <input type="submit" value="Update"/>
</form:form>



数据库表结构:
Create Table

CREATE TABLE customers (
ID int(11) NOT NULL AUTO_INCREMENT,
Name varchar(255) DEFAULT NULL,
ADDRESS varchar(255) DEFAULT NULL,
PHONE varchar(255) DEFAULT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gb2312

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值