[原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
新增操作:
思路:在index.jsp 页面下,点击Add New Customer 超链接,转向newcustomer.jsp 。在添加的操作有两个需求:
1.保证name的唯一性,也就是说,在新增的用户中,先进行查询name在数据库中是否存在。
若存在,则通过转发发送到newcustomer.jsp 一条提示信息;
若不存,则将请求的信息name,address,phone封装为一个Customer的对象,在通过调用customerDAO的save(),将信息添加到数据库,并且通过重定向的方式跳转到 success.jsp,加上提示 和 超链接 返回 querey.do
2.在index.jsp 页面要有回显的功能。也就是说不过成功添加还是不成功添加,都要将输入的信息显示到输入框中,达到回显的功能。
关键代码:
1.index.jsp 中的 Add New Customer 超链接
<body>
<form action="query.do" method="post">
<table>
<tr>
<td>CustomerName:</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="Query"/></td>
<td><a href="newcustomer.jsp">Add New Customer</a></td>
</tr>
</table>
</form>
<br><br>
2.newcustomer.jsp 页面
说明:① 通过 value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>" 方式 达到回显的效果
② 当添加的name 在数据库从在,servlet 会返回一个message 信息。通过 <h2><font color="red"><%=request.getAttribute("message") == null ? "" : request.getAttribute("message")%></font></h2> 方式
达到提示的效果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<body>
<%--可以用来回显 <%= request.getParameter("name") %> 若为空 则赋为 空串儿 ; 反之 ,赋原值 --%> <h2><font color="red"><%=request.getAttribute("message") == null ? "" : request.getAttribute("message")%></font></h2> <form action="addCustomer.do" method="post">
<table>
<tr>
<td>CustomerName:</td>
<td><input type="text" name="name"
value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>"/></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address"
value="<%= request.getParameter("address") == null ? "" : request.getParameter("address")%>"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone"
value="<%= request.getParameter("phone") == null ? "" : request.getParameter("phone") %>"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="submit"/></td>
</tr>
</table>
</form> </body>
</html>
3.success,jsp :提示信息和返回index.jsp 的超链接
<body>
<h1>操作成功</h1>
<a href="index.jsp">return ....</a>
</body>
3.CustomerServlet2 中的addCustomer()方法
private void addCustomer(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { System.out.println("add function");
request.setCharacterEncoding("UTF-8");
// 1.获取表单参数:name,address,phone
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone"); // 2.检验 name 是否已经被占用
// 2.1调用 CustomerDAO 的 getCountWithName(String name) 获取 name 在数据库中是否存在 long rel = customerDAO.getCountWithName(name);
// 2.2 若返回值大于0,则响应newcustomer.jsp 页面:
if (rel > 0) {
// 2.2.1 要求在newcustomer.jsp 页面显示一条消息:用户名 name 已经被占用了,请重新选择
// 在request 中放入一个属性 message:用户名 name
// 已经被占用,请重新选择!在页面通过request.getAttribute("message") 的方式来显示
// 2.2.2 newcustomer.jsp 的表单值可以回显
// value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>"
// 进行回显
request.setAttribute("message", "用户名 " + name + " 已经被占用了,请重新选择!");
// 通过转发的方式来响应 newcustomer.jsp
request.getRequestDispatcher("/newcustomer.jsp").forward(request,
response);
// 2.2.3 结束方法:return
return;
}
// 3.把表单参数封装为一个Customer 对象
Customer customer = new Customer(name, address, phone); // 4.调用 customerDAO 的 save(Customer customer) 方法执行保存
customerDAO.save(customer); // 5.重定向到success.jsp 页面:使用重定向可以避免出现表单的重复提交
response.sendRedirect("success.jsp");
}
}
4.CustomerDAO 中的 save 声明 ,getCountWithName()
//保存操作
public void save(Customer coustomer); //查看与参数相同的名字有多少个记录数
public long getCountWithName(String name);
5.CustomerDAOJdbcImpl 对CustomerDAO 的 save()具体实现 , getCountWithName
@Override
public void save(Customer customer) {
String sql = "INSERT INTO customers(name, address, phone) VALUES(?,?,? )";
update(sql,customer.getName(),customer.getAddress(),customer.getPhone());
} @Override
public long getCountWithName(String name) {
String sql = "SELECT count(id) FROM customers WHERE name = ?";
return getForValue(sql, name);
}
6.DAO 中的update() ,getForValue
/**
* @param sql
* : sql语句
* @param ags
* : sql语句的占位符
* @description:该方法封装了 INSERT ,DELETE,UPDATE 操作
*/
public void update(String sql, Object... ags){ Connection connection = null;
try {
connection = JdbcUtils.getConnection();
queryRunner.update(connection, sql, ags); } catch (Exception e) {
e.printStackTrace();
} finally{
JdbcUtils.releaseConnection(connection);
}
} /**
* @param sql
* @param args
* @description:返回某一个字段的值,例如:返回某一条记录的customerName
*/
public <E> E getForValue(String sql, Object... args) { Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return (E) queryRunner.query(connection, sql, new ScalarHandler(), args); } catch (Exception e) {
e.printStackTrace();
} finally{
JdbcUtils.releaseConnection(connection);
}
return null;
}
7.总结
1)从代码层次理解MVC
2)对于添加:一方面 要验证;一方面要更新数据库
[原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现的更多相关文章
- [原创]java WEB学习笔记24:MVC案例完整实践(part 5)---删除操作的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记20:MVC案例完整实践(part 1)---MVC架构分析
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记23:MVC案例完整实践(part 4)---模糊查询的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
随机推荐
- win10 microsoft edge 浏览器收藏夹位置
1.打开文件夹,找到(注意 用户名 改为你自己的用户名) C:\Users\用户名\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bb ...
- different between method and function
A method is on an object. A function is independent of an object. For Java, there are only methods. ...
- weex 学习
相关资料和链接: # 官方网站https://weex.apache.org/cn/ # githubhttps://github.com/apache/incubator-weex # weex环境 ...
- SpringSecurity学习三----------通过Security标签库简单显示视图
© 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...
- debian SSD ext4 4K 对齐
新入手了一台thinkpad, 原来的机械硬盘是500G的, 于是购入一块镁光的MX200 250G的SSD来新装debian stable(jessie) 1, 安装系统的之前按住F1进入bios后 ...
- C语言小板凳(1)
①strlen()函数作用:计算字符串的长度,当遇到"\n"字符时结束,即遇到数值"0"时结束计算,有一点特别要注意当这个函数用来计算数组的长度的时候遇到数值0 ...
- springboot 中使用AOP
网上关于AOP的例子好多,各种名词解释也一大堆,反正名词各种晦涩,自己写个最最最简单的例子入门mark一下,以后再深入学习. maven依赖 <dependency> <groupI ...
- linux实用命令备忘
1. 卸载旧内核 sudo apt-get purge linux-image-xxx-xx-generic 2. 快速换ubuntu的源: sudo sed -i 's/vivid/wily/' / ...
- 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...
- args *args **kwargs区别
python 函数中的参数类型有两种,分别为 位置参数和关键字参数: 一 .位置参数(该类参数位置固定不变) args: 表示默认位置参数,该参数是具象的,有多少个参数就传递多少参数,且参数位 ...