显示新闻展示、增加、删除功能

新闻展示

welcome.jsp页面中展示新闻列表(部分代码)

<!-- 商品列表展示 -->
<table>
<tr>
	<td>商品编号</td><td>商品名称</td><td>商品产地</td><td>商品类型</td><td>商品进价</td><td>商品售价</td><td>商品库存</td><td>编辑|删除</td>
</tr>
<%
//	获取商品列表(类型强转)
List<Commodity> cList = (List<Commodity>)session.getAttribute("cList");
if(cList != null && cList.size() > 0){
	for(Commodity c:cList){
%>
	<tr>
		<td><%=c.getC_id() %></td><td><%=c.getC_name() %></td><td><%=c.getC_madein() %></td><td><%=c.getCt().getCt_name() %></td><td><%=c.getC_inprice() %></td><td><%=c.getC_outprice() %></td><td><%=c.getC_num() %></td><td>编辑|删除</td>
	</tr>
<%
	}
}
%>
</table>

commoditytypeDao接口的抽象方法,根据商品种类编号,返回商品种类类型,供商品表使用

public interface CommoditytypeDao {
	Commoditytype getCommoditytypeById(String ct_id);
}

commoditytypeDaoImpl实现类,实现接口方法

@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

CommoditytypeController控制层传递信息

public class CommoditytypeController {
	private CommoditytypeDao commoditytypeDao = null;
	public CommoditytypeController() {
		commoditytypeDao = new CommoditytypeDaoImpl();
	}
}

CommodityDao接口中,添加方法,返回商品表所有信息的集合

public interface CommodityDao {
	List<Commodity> getCommodityList();
}

CommodityDaoImpl实现类实现接口的方法

@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

CommodityController控制层

public class CommodityController {
	private CommodityDao commodityDao = null;
	public CommodityController() {
		commodityDao = new CommodityDaoImpl();
	}
	public List<Commodity> getCommodityList() {
		return commodityDao.getCommodityList();
	}
}

在LoginServlet中用session保存,在welcome中读取并显示

List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);

增加、删除功能

CommoditytypeDao接口

public interface CommoditytypeDao {
	Commoditytype getCommoditytypeById(String ct_id);
	// 用于下拉列表读取商品类型名称
	List<Commoditytype> getCommoditytypeList();
}

CommoditytypeDaoImpl实现类

public class CommoditytypeDaoImpl implements CommoditytypeDao{

	@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

	@Override
	public List<Commoditytype> getCommoditytypeList() {
		List<Commoditytype> cts = new ArrayList<>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commoditytype ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
				cts.add(ct);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return cts;
	}
}

CommoditytypeController 控制层

public class CommoditytypeController {
	private CommoditytypeDao commoditytypeDao = null;
	public CommoditytypeController() {
		commoditytypeDao = new CommoditytypeDaoImpl();
	}
	
	public List<Commoditytype> getCommoditytypeList(){
		return commoditytypeDao.getCommoditytypeList();
	}
}

CommodityDao 接口

public interface CommodityDao {
	List<Commodity> getCommodityList();
	int istCommodity(Commodity c);
	int delCommodityById(String c_id);
}

CommodityDaoImpl实现类

public class CommodityDaoImpl implements CommodityDao {

	@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

	@Override
	public int istCommodity(Commodity c) {
		int line = 0;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "insert into commodity(c_id,c_name,c_madein,c_type,c_inprice,c_outprice,c_num) values (?,?,?,?,?,?,?)";
			List param = new ArrayList();
			param.add(c.getC_id());
			param.add(c.getC_name());
			param.add(c.getC_madein());
			param.add(c.getC_type());
			param.add(c.getC_inprice());
			param.add(c.getC_outprice());
			param.add(c.getC_num());
			
			line = DBHelper.executeUpdate(conn, sql, param);
			
			DBHelper.closeConnection(conn);
		}catch(Exception  e) {
			e.printStackTrace();
		}
		return line;
	}

	@Override
	public int delCommodityById(String c_id) {
		int line = 0;
		Connection conn = DBHelper.getConnection();
		String sql = "delete from commodity where c_id=?";
		List param = new ArrayList();
		param.add(c_id);
		line = DBHelper.executeUpdate(conn, sql, param);
		DBHelper.closeConnection(conn);
		return line;
	}
	
}

CommodityController 控制层

public class CommodityController {
	private CommodityDao commodityDao = null;
	public CommodityController() {
		commodityDao = new CommodityDaoImpl();
	}
	
	public List<Commodity> getCommodityList() {
		return commodityDao.getCommodityList();
	}
	
	public int istCommodity(String c_name,String c_madein,String c_type,Integer c_inprice,Integer c_outprice,Integer c_num) {
		Commodity c = new Commodity(System.currentTimeMillis()+"",c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		return commodityDao.istCommodity(c);
	}
	
	public int delCommodityById(String c_id) {
		return commodityDao.delCommodityById(c_id);
	}
}

welcome.jsp页面(部分代码)

<form method="post" action="/web06/istCommodityServlet">
<table style="border: 1px solid black; margin:0 auto;">
<td colspan="1" style="width:130px;" >商品名称:<input type="text" name="c_name" /></td>
<td style="width:150px;" >商品产地:<input type="text" name="c_madein" /></td>
<td style="width:130px;" >商品类型:
	<select name="c_type">
		<%
		for(Commoditytype ct:ctList){
		%>
		<option value="<%=ct.getCt_id() %>"><%=ct.getCt_name() %></option>
		<%
		}
		%>
	</select>
</td>
<td style="width:130px;" >商品进价:<input type="text" name="c_inprice" /></td>
<td style="width:130px;" >商品售价:<input type="text" name="c_outprice" /></td>
<td style="width:130px;" >商品库存:<input type="text" name="c_num" /></td>
<td style="width:130px;" ><input type="submit" name="新增" /></td>

<!-- 商品列表展示 -->
<tr>
	<td>商品编号</td><td>商品名称</td><td>商品产地</td><td>商品类型</td><td>商品进价</td><td>商品售价</td><td>商品库存</td><td>编辑|删除</td>
</tr>
<%
if(cList != null && cList.size() > 0){
	for(Commodity c:cList){
%>
	<tr>
		<td><%=c.getC_id() %></td><td><%=c.getC_name() %></td><td><%=c.getC_madein() %></td><td><%=c.getCt().getCt_name() %></td><td><%=c.getC_inprice() %></td><td><%=c.getC_outprice() %></td><td><%=c.getC_num() %></td><td><a href="/web06/toupdateCommodityByid?c_id=<%=c.getC_id() %>">编辑</a>|<a href="/web06/deleteCommodityById?c_id=<%=c.getC_id() %>">删除</a></td>
	</tr>
<%
	}
}
%>
</table>
</form>

Servlet(ist\del)

public class IstCommodityServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_name = req.getParameter("c_name");
		String c_madein = req.getParameter("c_madein");
		String c_type = req.getParameter("c_type");
		Integer c_inprice = new Integer(req.getParameter("c_inprice"));
		Integer c_outprice = new Integer(req.getParameter("c_outprice"));
		Integer c_num = new Integer(req.getParameter("c_num"));
		
		int line = new CommodityController().istCommodity(c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
public class DeleteCommodityById extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_id = req.getParameter("c_id");
		
		int line = new CommodityController().delCommodityById(c_id);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

你可能感兴趣的:(jsp)