一、添加一个“商品类型维护”
1、menuBar.jsp中添加下拉表
<li><a href="initCommodityType">商品类型维护</a></li>
2、在sqlMapConfig.xml中添加
<sqlMap resource="CommodityTypeSqlMap.xml" />
3、各页面的制作(commodityTypeList.jsp addCommodityType.jsp editCommodityType.jsp)
commodityTypeList.jsp 用于显示初始化列表
<table class="table table-striped table-bordered bootstrap-datatable datatable responsive"> <thead> <tr> <th>商 品 ID </th> <th>商品名称</th> <th> 备 注</th> <th> 操 作</th> </tr> </thead> <tbody> <c:forEach var="commodityTypeInfo" items="${list}" varStatus="sts"> <tr> <td>${commodityTypeInfo.commodity_typeId}</td> <td>${commodityTypeInfo.commodity_typeName}</td> <td>${commodityTypeInfo.commodity_typeNote}</td> <td class="center"> <a class="btn btn-info" href="initEditCommodityType?commodity_typeId=${commodityTypeInfo.commodity_typeId}"> <i class="glyphicon glyphicon-edit icon-white"></i> 编辑</a> <a class="btn btn-danger" onclick="showDialog('delCommodityType?commodity_typeId=${commodityTypeInfo.commodity_typeId}')" href="#" data-toggle="modal" data-target="#myModal"> <i class="glyphicon glyphicon-trash icon-white"></i> 删除</a> </td> </tr> </c:forEach> </tbody>
addCommodityType.jsp 用于添加商品类型
<form:form modelAttribute="commodityTypeForm" action="addCommodityType" method="post"> <div class="alert alert-info">${message}<form:errors path="*"></form:errors></div> <table class="table table-bordered responsive"> <tr> <td style="background-color: #f9f9f9;">商品名称<span style="color:red;">*</span></td> <td><form:input path="commodity_typeName" cssClass="form-control" cssErrorClass="form-control error" value="${commodityTypeForm.commodity_typeName}" /></td> </tr> <tr> <td style="background-color: #f9f9f9;">备注</td> <td colspan="3"><input name="commodity_typeNote" class="form-control" value="${commodityTypeForm.commodity_typeNote}" /></td> </tr> </table> <div class="input-group has-success col-md-12" align="right"> <button type="submit" class="btn btn-success">提 交</button> <a class="btn btn-danger" href="initCommodityType"> 取 消</a> </div> </form:form>
editCommodityType.jsp 用于更新商品类型
<form:form modelAttribute="commodityTypeForm" action="editCommodityType" method="post"> <form:hidden path="commodity_typeId" value="${commodityTypeForm.commodity_typeId}" /> <div class="alert alert-info">${message}<form:errors path="*"></form:errors></div> <table class="table table-bordered responsive"> <tr> <td style="background-color: #f9f9f9;">商品ID</td> <td>${commodityTypeForm.commodity_typeId}</td> <td style="background-color: #f9f9f9;">商品名称</td> <td><input name="commodity_typeName" class="form-control" value="${commodityTypeForm.commodity_typeName}" /></td> </tr> <tr> <td style="background-color: #f9f9f9;">备注</td> <td><input name="commodity_typeNote" class="form-control" value="${commodityTypeForm.commodity_typeNote}" /></td> </tr> </table> <div class="input-group has-success col-md-12" align="right"> <button type="submit" class="btn btn-success">提 交</button> <!-- <button type="button" onclick="window.history.back();" class="btn btn-danger">取 消</button> --> <a class="btn btn-danger" href="initCommodityType"> 取 消</a> </div> </form:form>
4、CommodityTypeController.java 的编辑
商品类型初始化
@RequestMapping(value = "initCommodityType", method = RequestMethod.GET) public String initCommodityType(Model model) { log.info("商品类型初始化"); model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; }
追加商品类型
@RequestMapping(value = "initAddCommodityType", method = RequestMethod.GET) public String initAddCommodityType(Model model) { log.info("追加商品类型初始化"); CommodityTypeForm commodityTypeForm = new CommodityTypeForm(); model.addAttribute("commodityTypeForm", commodityTypeForm); return "manager/commodityType/addCommodityType"; } @RequestMapping(value = "addCommodityType", method = RequestMethod.POST) public String executeAddCommodityType(Model model, HttpSession session, @Valid @ModelAttribute("commodityTypeForm") CommodityTypeForm commodityTypeForm, BindingResult results) throws SQLException, IOException { if (results.hasErrors()) { log.info("添加商品类型信息"); model.addAttribute("commodityTypeForm", commodityTypeForm); return "manager/commodityType/addCommodityType"; } Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); commodityTypeForm.setUpdateTime(dateformat.format(date)); boolean result = commodityTypeService.addCommodityType(commodityTypeForm); if(!result) { throw new SQLException("商品类型信息添加失败!"); } model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; }
修改商品类型
@RequestMapping(value = "initEditCommodityType", method = RequestMethod.GET) public String initEditCommodityType(Model model, CommodityTypeForm commodityTypeForm) { log.info("修改商品类型初始化"); CommodityTypeForm result = commodityTypeService.searchCommodityType(commodityTypeForm); model.addAttribute("commodityTypeForm", result); return "manager/commodityType/editCommodityType"; } @RequestMapping(value = "editCommodityType", method = RequestMethod.POST) public String executeEditCommodityType(Model model,HttpSession session, @Valid @ModelAttribute("commodityTypeForm") CommodityTypeForm commodityTypeForm,BindingResult results) throws SQLException { if (results.hasErrors()) { log.info("修改商品类型信息"); CommodityTypeForm result = commodityTypeService.searchCommodityType(commodityTypeForm); model.addAttribute("commodityTypeForm", result); /** List<Item> commodityTypeList = itemListComponent.getCommodityTypeList(); model.addAttribute("commodityTypeList", commodityTypeList); */ model.addAttribute("commodityTypeForm", commodityTypeForm); return "manager/commodityType/editCommodityType"; } log.info("修改商品类型信息"); /** UVO uvo = (UVO)session.getAttribute("UVO"); stockForm.setUpdateUser(uvo.getUserName()); */ Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); commodityTypeForm.setUpdateTime(dateformat.format(date)); boolean result = commodityTypeService.editCommodityType(commodityTypeForm); if(!result) { throw new SQLException("商品类型信息更新失败!"); } model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; }
删除商品类型
@RequestMapping(value = "delCommodityType", method = RequestMethod.GET) public String executeDelCommodityType(Model model, CommodityTypeForm commodityTypeForm) throws SQLException { log.info("删除商品类型信息"); boolean result = commodityTypeService.delCommodityType(commodityTypeForm); if(!result) { throw new SQLException("商品类型信息删除失败!"); } model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; }
5、CommodityTypeService.java 的编辑
public List<CommodityTypeForm> searchCommodityTypeList() { List<CommodityTypeForm> result = queryDao.executeForObjectList( "CommodityType.selectCommodityTypeList", null); return result; } public CommodityTypeForm searchCommodityType(CommodityTypeForm frm) { CommodityTypeForm result = queryDao.executeForObject( "CommodityType.selectCommodityType", frm, CommodityTypeForm.class); return result; } public boolean editCommodityType(CommodityTypeForm frm) { int result = updateDao.execute("CommodityType.editCommodityType", frm); if (result == 1) { return true; } return false; } public boolean delCommodityType(CommodityTypeForm frm) { int result = updateDao.execute("CommodityType.delCommodityType", frm); if (result == 1) { return true; } return false; } public boolean addCommodityType(CommodityTypeForm frm) { Integer sequee = queryDao.executeForObject("CommodityType.getSeq", null, Integer.class); String commodity_typeId = frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee); frm.setCommodity_typeId(commodity_typeId); int result = updateDao.execute("CommodityType.addCommodityType", frm); if (result == 1) { return true; } return false; }
6、CommodityTypeSqlMap.xml 的编辑
<sqlMap namespace="CommodityType"> <select id="selectCommodityTypeList" parameterClass="cn.agriculture.web.form.CommodityTypeForm" resultClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type_id as commodity_typeId, commodity_type_name as commodity_typeName, commodity_type_note as commodity_typeNote FROM commodity_type </select> <select id="selectCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm" resultClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type_id as commodity_typeId, commodity_type_name as commodity_typeName, commodity_type_note as commodity_typeNote FROM commodity_type WHERE commodity_type_id = #commodity_typeId# </select> <insert id="addCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> INSERT INTO commodity_type(commodity_type_id,commodity_type_name,commodity_type_note) VALUES(#commodity_typeId#,#commodity_typeName#,#commodity_typeNote#) </insert> <select id="getSeq" resultClass="java.lang.Integer"> SELECT _nextval('commodityTypeId') </select> <update id="editCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> UPDATE commodity_type SET commodity_type_name = #commodity_typeName#, commodity_type_note = #commodity_typeNote# WHERE commodity_type_id = #commodity_typeId# </update> <delete id="delCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> DELETE FROM commodity_type WHERE commodity_type_id = #commodity_typeId# </delete> </sqlMap>