标签:
一 验证不能为空的操作:
在form中添加代码@NotEmpty(field="商品类型", message="{errors.required}")
在controller中对应不能为空的位置添加
if(results.hasErrors())
return "manager/commodityType/addCommodityType";
3.不能为空的格式:<td style="background-color: #f9f9f9;">商品名称</td>
<td><form:input path="commodityTypeName" cssClass="form-control" cssErrorClass="form-control error" value="${commodityTypeForm.commodityTypeName}"/></td>
二 添加商品类型模块
点击商品类型管理的按钮进入一个列表界面,显示数据库中已经存在的商品类型列表,本模块的功能有编辑,删除,追加和取消。
点击编辑按钮进入对商品类型的编辑,输入内容为空或添加商品类型与已有类型相同时提示错误信息。
点击追加按钮进入追加页面添加时,同样,输入内容为空或添加商品类型与已有类型相同时提示错误信息。
后台建立addCommodityType.jsp
commodityTypeList.jsp
editCommodityType.jsp这三个页面,添加ID,商品名称和备注:commodity_type_id,commodity_type_name,commodity_type_note
在CommodityTypeForm中的重要代码:
为需要进行vailudation的属性添加相应的标签比如这里的Name需要设置为不能为空或者不能与数据库中已有的数据重复:
public class CommodityTypeForm {
private String commodityTypeId;
//验证不能为空
@NotEmpty(field="商品类型", message="{errors.required}")
private String commodityTypeName;
private String commodityTypeNote;
private String updateTime;
private String updateUser;
}
3.在CommodityTypeSqlMap中的重要代码:数据库中所要进行的一些操作,如查询,插入,更新数据的语句
<sqlMap namespace="Commodity_type"> <select id="selectCommodityTypeList" parameterClass="cn.agriculture.web.form.CommodityTypeForm" resultClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type.commodity_type_id as commodityTypeId, commodity_type.commodity_type_name as commodityTypeName, commodity_type.commodity_type_note as commodityTypeNote FROM commodity_type </select> <select id="selectCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm" resultClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type_id as commodityTypeId, commodity_type_name as commodityTypeName, commodity_type_note as commodityTypeNote FROM commodity_type WHERE commodity_type_id = #commodityTypeId# </select> <select id="getSeq" resultClass="java.lang.Integer"> SELECT _nextval(‘commodityTypeId‘) </select> <insert id="addCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> INSERT INTO commodity_type(commodity_type_id, commodity_type_name,commodity_type_note) VALUES(#commodityTypeId#, #commodityTypeName#,#commodityTypeNote#) </insert> <update id="editCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> UPDATE commodity_type SET commodity_type_name = #commodityTypeName#, commodity_type_note = #commodityTypeNote# WHERE commodity_type_id = #commodityTypeId# </update> <delete id="delCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> DELETE FROM commodity_type WHERE commodity_type_id = #commodityTypeId# </delete> </sqlMap>
4.在CommodityTypeService中的重要代码:调用存放数据库中数据的一些方法
@Service public class CommodityTypeService { @Autowired QueryDAO queryDao; @Autowired UpdateDAO updateDao; public List<CommodityTypeForm> searchCommodityTypeList() { List<CommodityTypeForm> result = queryDao.executeForObjectList("Commodity_type.selectCommodityTypeList", null); return result; } public CommodityTypeForm searchCommodityType(CommodityTypeForm frm) { CommodityTypeForm result = queryDao.executeForObject("Commodity_type.selectCommodityType", frm, CommodityTypeForm.class); return result; } public boolean addCommodityType(CommodityTypeForm frm) { Integer sequee = queryDao.executeForObject("Commodity_type.getSeq", null, Integer.class); String commodityTypeId = frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee); frm.setCommodityTypeId(commodityTypeId); int result = updateDao.execute("Commodity_type.addCommodityType", frm); if (result == 1) { return true; } return false; } public CommodityTypeForm getEditCommodityType(CommodityTypeForm frm) { return queryDao.executeForObject("Commodity_type.selectEditCommodityType", frm,CommodityTypeForm.class); } public boolean editCommodityType(CommodityTypeForm frm) { int result = updateDao.execute("Commodity_type.editCommodityType", frm); if (result == 1) { return true; } return false; } public boolean delCommodityType(CommodityTypeForm frm) { int result = updateDao.execute("Commodity_type.delCommodityType", frm); if (result == 1) { return true; } return false; } }
5.在CommodityTypeControllor中的重要代码:主要实现各页面的跳转和一些功能
@Slf4j @Controller("CommodityTypeController") @RequestMapping("/") public class CommodityTypeController { @Autowired CommodityTypeService commodityTypeService; @Autowired ItemListComponent itemListComponent; @Autowired private Environment env; @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()) return "manager/commodityType/addCommodityType"; log.info("添加商品类型信息"); UVO uvo = (UVO) session.getAttribute("UVO"); commodityTypeForm.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 .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, IOException { //判断文本框内容不能为空 if(results.hasErrors()) return "manager/commodityType/editCommodityType"; log.info("修改商品类型信息"); //uvo语句是判断登陆时间,登陆超时将重新登陆 // UVO uvo = (UVO)session.getAttribute("UVO"); // commodityTypeForm.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"; } }
标签:
原文地址:http://my.oschina.net/u/2411770/blog/487573