知识点
MVC模型:
实现步骤:分为3级
1.M(Model) 持久层 代码与数据库进行交互的代码(Mybatis-dao层)
2.C(Control) 控制层 完成某项业务的具体操作过程(Controller层----Service层)
3.V(View) 视图层 一般指用户看到的内容(页面)
项目目录
.filter //过滤器 解决中文字符集乱码√
.util //数据库连接工具类√
.pojo //实体类√
.dao //数据操作类√-------M数据访问层
.servlet //servlet类√
service层:服务层√
1.数据库
2.filter下新建一个EncodingFilter用来解决中文字符集乱码
3.web.xml下进行对EncodingFilter相应的配置
4.在util下新建一个类用来处理对数据库的连接操作
5.在pojo下新建一个Provider实体类
pojo---provider
package com.baidou.pojo;
import java.util.Date;
/**
* 供应商实体类
*/
public class Provider {
private Integer id; //id
private String proCode; //供应商编码
private String proName; //供应商名称
private String proDesc; //供应商描述
private String proContact; //供应商联系人
private String proPhone; //供应商电话
private String proAddress; //供应商地址
private String proFax; //供应商传真
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate;//更新时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
5.在dao下新建一个ProviderDao接口 以及对应的方法实现类(使用接口类是为了规范开发)
dao---provider---ProviderDao
package com.baidou.dao.provider;
import com.baidou.pojo.Provider;
import java.sql.Connection;
import java.util.List;
public interface ProviderDao {
//增加供应商
public int add(Connection connection, Provider provider)throws Exception;
// 通过供应商名称、编码获取供应商列表-模糊查询-providerList
public List<Provider> getProviderList(Connection connection, String proName, String proCode)throws Exception;
//通过proId删除Provider
public int deleteProviderById(Connection connection, String delId)throws Exception;
//通过proId获取Provider
public Provider getProviderById(Connection connection, String id)throws Exception;
//修改用户信息
public int modify(Connection connection, Provider provider)throws Exception;
}
6.新建ProviderDaoImpl.java 实现ProviderDao接口,及未实现的方法
dao---provider---ProviderDaoimpl
package com.baidou.dao.provider;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.baidou.dao.BaseDao;
import com.baidou.pojo.Provider;
import com.mysql.jdbc.StringUtils;
/**
* Provider数据持久层
*/
public class ProviderDaoImpl implements ProviderDao {
//添加供应商
public int add(Connection connection, Provider provider)
throws Exception {
// TODO Auto-generated method stub
PreparedStatement pstm = null;
int flag = 0;
if(null != connection){
String sql = "insert into smbms_provider (proCode,proName,proDesc," +
"proContact,proPhone,proAddress,proFax,createdBy,creationDate) " +
"values(?,?,?,?,?,?,?,?,?)";
Object[] params = {provider.getProCode(),provider.getProName(),provider.getProDesc(),
provider.getProContact(),provider.getProPhone(),provider.getProAddress(),
provider.getProFax(),provider.getCreatedBy(),provider.getCreationDate()};
flag = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null, pstm, null);
}
return flag;
}
//通过条件查询获取供应商列表
public List<Provider> getProviderList(Connection connection, String proName,String proCode)
throws Exception {
// TODO Auto-generated method stub
PreparedStatement pstm = null;
ResultSet rs = null;
List<Provider> providerList = new ArrayList<Provider>();
if(connection != null){
StringBuffer sql = new StringBuffer();
sql.append("select * from smbms_provider where 1=1 ");
List<Object> list = new ArrayList<Object>();
if(!StringUtils.isNullOrEmpty(proName)){
sql.append(" and proName like ?");
list.add("%"+proName+"%");
}
if(!StringUtils.isNullOrEmpty(proCode)){
sql.append(" and proCode like ?");
list.add("%"+proCode+"%");
}
Object[] params = list.toArray();
System.out.println("sql ----> " + sql.toString());
rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);
while(rs.next()){
Provider _provider = new Provider();
_provider.setId(rs.getInt("id"));
_provider.setProCode(rs.getString("proCode"));
_provider.setProName(rs.getString("proName"));
_provider.setProDesc(rs.getString("proDesc"));
_provider.setProContact(rs.getString("proContact"));
_provider.setProPhone(rs.getString("proPhone"));
_provider.setProAddress(rs.getString("proAddress"));
_provider.setProFax(rs.getString("proFax"));
_provider.setCreationDate(rs.getTimestamp("creationDate"));
providerList.add(_provider);
}
BaseDao.closeResource(null, pstm, rs);
}
return providerList;
}
//删除供应商信息
public int deleteProviderById(Connection connection, String delId)
throws Exception {
// TODO Auto-generated method stub
PreparedStatement pstm = null;
int flag = 0;
if(null != connection){
String sql = "delete from smbms_provider where id=?";
Object[] params = {delId};
flag = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null, pstm, null);
}
return flag;
}
//通过id获取供应商
public Provider getProviderById(Connection connection, String id)
throws Exception {
// TODO Auto-generated method stub
Provider provider = null;
PreparedStatement pstm = null;
ResultSet rs = null;
if(null != connection){
String sql = "select * from smbms_provider where id=?";
Object[] params = {id};
rs = BaseDao.execute(connection, pstm, rs, sql, params);
if(rs.next()){
provider = new Provider();
provider.setId(rs.getInt("id"));
provider.setProCode(rs.getString("proCode"));
provider.setProName(rs.getString("proName"));
provider.setProDesc(rs.getString("proDesc"));
provider.setProContact(rs.getString("proContact"));
provider.setProPhone(rs.getString("proPhone"));
provider.setProAddress(rs.getString("proAddress"));
provider.setProFax(rs.getString("proFax"));
provider.setCreatedBy(rs.getInt("createdBy"));
provider.setCreationDate(rs.getTimestamp("creationDate"));
provider.setModifyBy(rs.getInt("modifyBy"));
provider.setModifyDate(rs.getTimestamp("modifyDate"));
}
BaseDao.closeResource(null, pstm, rs);
}
return provider;
}
//修改供应商信息
public int modify(Connection connection, Provider provider)
throws Exception {
// TODO Auto-generated method stub
int flag = 0;
PreparedStatement pstm = null;
if(null != connection){
String sql = "update smbms_provider set proName=?,proDesc=?,proContact=?," +
"proPhone=?,proAddress=?,proFax=?,modifyBy=?,modifyDate=? where id = ? ";
Object[] params = {provider.getProName(),provider.getProDesc(),provider.getProContact(),provider.getProPhone(),provider.getProAddress(),
provider.getProFax(),provider.getModifyBy(),provider.getModifyDate(),provider.getId()};
flag = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null, pstm, null);
}
return flag;
}
}
7.在servlet下创建ProviderServlet用来实现对供应商登录的操作
package com.baidou.servlet.provider;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONArray;
import com.baidou.pojo.Provider;
import com.baidou.pojo.User;
import com.baidou.service.provider.ProviderService;
import com.baidou.service.provider.ProviderServiceImpl;
import com.baidou.util.Constants;
import com.mysql.jdbc.StringUtils;
/**
* 供应商Controller,实现servlet复用
*/
public class ProviderServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过路由调用相应的业务
String method = request.getParameter("method");
System.out.println("method--->" + method);
if (method != null && method.equals("query")) {
this.query(request, response);
} else if (method != null && method.equals("add")) {
this.add(request, response);
} else if (method != null && method.equals("view")) {
this.getProviderById(request, response, "providerview.jsp");
} else if (method != null && method.equals("modify")) {
this.getProviderById(request, response, "providermodify.jsp");
} else if (method != null && method.equals("modifysave")) {
this.modify(request, response);
} else if (method != null && method.equals("delprovider")) {
this.delProvider(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
/**
* 删除供应商信息
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void delProvider(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("proid");
HashMap<String, String> resultMap = new HashMap<String, String>();
if (!StringUtils.isNullOrEmpty(id)) {
ProviderService providerService = new ProviderServiceImpl();
int flag = providerService.deleteProviderById(id);
if (flag == 0) {//删除成功
resultMap.put("delResult", "true");
} else if (flag == -1) {//删除失败
resultMap.put("delResult", "false");
} else if (flag > 0) {//该供应商下有订单,不能删除,返回订单数
resultMap.put("delResult", String.valueOf(flag));
}
} else {
resultMap.put("delResult", "notexit");
}
//把resultMap转换成json对象输出
response.setContentType("application/json");
PrintWriter outPrintWriter = response.getWriter();
outPrintWriter.write(JSONArray.toJSONString(resultMap));
outPrintWriter.flush();
outPrintWriter.close();
}
/**
* 修改供应商信息
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void modify(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String proContact = request.getParameter("proContact");
String proPhone = request.getParameter("proPhone");
String proAddress = request.getParameter("proAddress");
String proFax = request.getParameter("proFax");
String proDesc = request.getParameter("proDesc");
String id = request.getParameter("id");
Provider provider = new Provider();
provider.setId(Integer.valueOf(id));
provider.setProContact(proContact);
provider.setProPhone(proPhone);
provider.setProFax(proFax);
provider.setProAddress(proAddress);
provider.setProDesc(proDesc);
provider.setModifyBy(((User) request.getSession().getAttribute(Constants.USER_SESSION)).getId());
provider.setModifyDate(new Date());
boolean flag = false;
ProviderService providerService = new ProviderServiceImpl();
flag = providerService.modify(provider);
if (flag) {
response.sendRedirect(request.getContextPath() + "/jsp/provider.do?method=query");
} else {
request.getRequestDispatcher("providermodify.jsp").forward(request, response);
}
}
/**
* 通过id获取供应商
*
* @param request
* @param response
* @param url
* @throws ServletException
* @throws IOException
*/
private void getProviderById(HttpServletRequest request, HttpServletResponse response, String url)
throws ServletException, IOException {
String id = request.getParameter("proid");
System.out.println("id-->" + id);
Provider provider = null;
if (!StringUtils.isNullOrEmpty(id)) {
ProviderService providerService = new ProviderServiceImpl();
provider = providerService.getProviderById(id);
request.setAttribute("provider", provider);
request.getRequestDispatcher(url).forward(request, response);
}
}
/**
* 添加供应商
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean flag = false;
String proCode = request.getParameter("proCode");
String proName = request.getParameter("proName");
String proContact = request.getParameter("proContact");
String proPhone = request.getParameter("proPhone");
String proAddress = request.getParameter("proAddress");
String proFax = request.getParameter("proFax");
String proDesc = request.getParameter("proDesc");
Provider provider = new Provider();
provider.setProCode(proCode);
provider.setProName(proName);
provider.setProContact(proContact);
provider.setProPhone(proPhone);
provider.setProFax(proFax);
provider.setProAddress(proAddress);
provider.setProDesc(proDesc);
provider.setCreatedBy(((User) request.getSession().getAttribute(Constants.USER_SESSION)).getId());
provider.setCreationDate(new Date());
ProviderService providerService = new ProviderServiceImpl();
flag = providerService.add(provider);
if (flag) {
response.sendRedirect(request.getContextPath() + "/jsp/provider.do?method=query");
} else {
request.getRequestDispatcher("provideradd.jsp").forward(request, response);
}
}
/**
* 获取供应商列表
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String queryProName = request.getParameter("queryProName");
String queryProCode = request.getParameter("queryProCode");
if (StringUtils.isNullOrEmpty(queryProName)) {
queryProName = "";
}
if (StringUtils.isNullOrEmpty(queryProCode)) {
queryProCode = "";
}
List<Provider> providerList = new ArrayList<Provider>();
ProviderService providerService = new ProviderServiceImpl();
providerList = providerService.getProviderList(queryProName, queryProCode);
request.setAttribute("providerList", providerList);
request.setAttribute("queryProName", queryProName);
request.setAttribute("queryProCode", queryProCode);
request.getRequestDispatcher("providerlist.jsp").forward(request, response);
}
}
有两点要注意的地方:
一:getParameter与getAttribute两者的区别
request.setAttribute("xiaoxi", "欢迎用户"+name);//向request域中放置信息 ( 键值对的形式) 名字为xiaoxi 内容为"欢迎用户"+name
request.getAttribute("xiaoxi");//得到request域中放置名字为xiaoxi的信息
request.getParameter("name");//得到request域的参数信息(得到jsp页面传过来的参数)
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 。
getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型
二:转发与重定向的区别
(1).重定向的执行过程:Web服务器向浏览器发送一个http响应--》浏览器接受此响应后再发送一个新的http请求到服务器--》服务器根据此请求寻找资源并发送给浏览器。它可以重定向到任意URL,不能共享request范围内的数据。
(2).重定向是在客户端发挥作用,通过新的地址实现页面转向。
(3).重定向是通过浏览器重新请求地址,在地址栏中可以显示转向后的地址。
(4).转发过程:Web服务器调用内部方法在容器内部完成请求和转发动作--》将目标资源发送给浏览器,它只能在同一个Web应用中使用,可以共享request范围内的数据。
(5).转发是在服务器端发挥作用,通过forward()方法将提交信息在多个页面间进行传递。
(6).转发是在服务器内部控制权的转移,客户端浏览器的地址栏不会显示出转向后的地址。
————————————————
如果本篇文章帮到了你,兄台何不留个言再走?什么!不想留言?不想留言也要点个赞吗,也算是缘分一场......什么!赞都不想点?管理员拿我的三十米大刀来!【左侧点击关注按钮,精彩好文不容错过】
原文链接:https://blog.csdn.net/qq_23994787/article/details/73612870
8.配置servlet在web.xml
9.新建jsp页面
webapp---jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/jsp/common/head.jsp"%>
<div class="right">
<div class="location">
<strong>你现在所在的位置是:</strong>
<span>供应商管理页面 >> 供应商添加页面</span>
</div>
<div class="providerAdd">
<form id="providerForm" name="providerForm" method="post" action="${pageContext.request.contextPath }/jsp/provider.do">
<input type="hidden" name="method" value="add">
<!--div的class 为error是验证错误,ok是验证成功-->
<div class="">
<label for="proCode">供应商编码:</label>
<input type="text" name="proCode" id="proCode" value="">
<!-- 放置提示信息 -->
<font color="red"></font>
</div>
<div>
<label for="proName">供应商名称:</label>
<input type="text" name="proName" id="proName" value="">
<font color="red"></font>
</div>
<div>
<label for="proContact">联系人:</label>
<input type="text" name="proContact" id="proContact" value="">
<font color="red"></font>
</div>
<div>
<label for="proPhone">联系电话:</label>
<input type="text" name="proPhone" id="proPhone" value="">
<font color="red"></font>
</div>
<div>
<label for="proAddress">联系地址:</label>
<input type="text" name="proAddress" id="proAddress" value="">
</div>
<div>
<label for="proFax">传真:</label>
<input type="text" name="proFax" id="proFax" value="">
</div>
<div>
<label for="proDesc">描述:</label>
<input type="text" name="proDesc" id="proDesc" value="">
</div>
<div class="providerAddBtn">
<input type="button" name="add" id="add" value="保存">
<input type="button" id="back" name="back" value="返回" >
</div>
</form>
</div>
</div>
</section>
<%@include file="/jsp/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/provideradd.js"></script>