Maven專案學習筆記(四)SSM專案利用《form:form》標籤在Mysql資料庫中實現新增和修改資料
近期自己搞好了系統的新增、修改、條件查詢的功能了,折騰了幾天,主要是新增修改的業務判斷比較多,導致那個負責修改的控制器程式碼加註釋都170多行,真第一次遇見這種情況。最後還是能實現對應的功能了,只是程式碼看起來十分難看,if、else判斷特別多,自己編碼水平還能提高吧。
一、新增功能
1.準備好前端的頁面,我的如下:
1.users_list.jsp:
部分程式碼
<div id="list_mean_div"> <form id="searchForm" name="searchForm" action="/user/search" method="post"> <div id="list_div_top"></div> <a id="name_a"><b>名字:</b></a> <input id="name_text" type="text" name="name" maxlength="20"/> <a id="class_a"><b>使用者型別:</b></a> <select id="class_select" name="userClass"> <option value="0">無</option> <option value="1">超級管理員</option> <option value="2">房東管理員</option> <option value="3">現任租客</option> <option value="4">前任租客</option> </select> <a id="house_a"><b>房號:</b></a> <select id="house_select" name="houseId"> <%--selected:預設選擇該選項;--%> <%--disabled:該選項不能被滑鼠選擇;(注:選項沒有被隱藏的時候)--%> <%--style="display:none":隱藏該選項;(注:該選項不會出現在下拉列)--%> <%--value="":該選項value為“”;(注:可做表單驗證)--%> <option selected disabled style="display:none" value="">選擇房號</option> <c:forEach items="${houseList}" var="hlist"> <option value="${hlist.houseId}"> ${hlist.houseId}</option> </c:forEach> </select> <button id="search_button" type="submit">搜尋</button> </form> <button id="insert_button" οnclick="javascript:window.location.href='/user/to_add';">新增人員</button> <br/><br/><br/> <hr/> </div>
2.users_add.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <jsp:include page="top.jsp"></jsp:include>
<section id="right"> <a id="userAdd_local"> 基本管理 -> 人員管理 -> 新增使用者 </a> <div id="user_add_div"> <form:form id="useaAddForm" name="addForm" action="/user/userAdd" method="post" modelAttribute="user"> <a id="useradd_acc_a">登入賬號</a> <form:input path="acc" id="useradd_acctext" placeholder="賬號..." maxlength="11" οnkeyup="this.value=this.value.replace(/[^\w\.\/]/ig,'')"/> <a id="acc_star">*</a> <%--<a id="acc_detail">賬號不能為空!</a>--%> <a id="useradd_name_a">姓名</a> <form:input path="name" placeholder="姓名..." maxlength="20" id="useradd_nametext"/> <a id="name_star">*</a> <%--<a id="name_detail">名字不能為空!</a>--%> <a id="useradd_userclass_a">使用者型別</a> <form:select path="userClass" items="${clist}" itemValue="classId" itemLabel="className" id="useradd_class_select"/> <a id="userclass_star">*</a> <%--<a id="userclass_detail">請選擇使用者型別!</a>--%> <a id="useradd_houseid_a">房間號</a> <form:select path="houseId" items="${hlist}" itemValue="id" itemLabel="houseId" id="useradd_houseid_select"/> <a id="useradd_houseid_star">*</a> <%--<a id="useradd_houseid_detail">請選擇房間號!</a>--%> <a id="useradd_ruzhutime_a">入住時間</a> <div class="layui-input-inline" > <form:input path="ruzhuTime" placeholder="yyyy-MM-dd" cssClass="layui-input" id="useraddtest1"/> </div> <a id="useradd_ruzhutime_star">*</a> <%--<a id="useradd_ruzhutime_detail">不能為空!</a>--%> <a id="useradd_outtime_a">搬出時間</a> <div class="layui-input-inline" > <form:input path="outTime" placeholder="yyyy-MM-dd" cssClass="layui-input" id="useraddtest2"/> </div> <%--<a id="useradd_outtime_detail">搬出時間不能在搬入時間前!</a>--%> <button id="submit_btn" type="submit">新增</button> <c:if test="${not empty error1}"> <a id="useradd_error1"><c:out value="${error1}" /></a> </c:if> </form:form> <button id="return_btn" οnclick="javascript:window.history.go(-1)">返回</button> </div> </section>
<jsp:include page="buttom.jsp"></jsp:include>
知識點:
<form:form>標籤除了有action的路徑和method請求的型別外還必需要有modelAttribute屬性。modelAttribute的屬性值對應的是要新增的資料的實體類,我新增的是使用者這個實體類,實體類的名稱是:User。在你點選正式的新增按鈕提交到後臺的時候,spring會自動幫你封裝實體的資料的,但是需要你在輸入框的屬性中新增path屬性,path屬性中的值就是對應實體類的某個欄位。不過在你沒有跳轉到新增使用者的介面之前,你必須要在路由控制器中新增一個User物件到新增使用者的介面,否則會報錯!
2.控制器層讓頁面實現跳轉
在users_list.jsp這個頁面的新增按鈕的onclick提交的是:/user/to_add,這個路由
οnclick="javascript:window.location.href='/user/to_add';"
我的是UserController這個類進行接收請求的,對應的路由如下。
@RequestMapping(value = "/to_add") public String toAdd(Map<String,Object> map){ map.put("user",new User()); map.put("clist",userClassBiz.selectAll()); map.put("hlist",houseBiz.selectAllHouse()); return "users_add"; }
在這個類的前面我是添加了路由的,所以路由沒問題。
@RequestMapping(value = "/user") public class UserController {
我在新增使用者介面中添加了user物件,使用者的型別列表和房間號列表,用於顯示資料。
3.最後的新增操作
userAdd方法:
@RequestMapping(value = "/userAdd") public ModelAndView userAdd(User user){ ModelAndView modelAndView = new ModelAndView(); if (!user.getAcc().equals("")&&!user.getName().equals("")&&!user.getHouseId().equals("")&&user.getUserClass()!=0&&user.getRuzhuTime()!=null){ /** * 1.判斷賬號是否獨一無二 */ String acc = user.getAcc(); if (userService.selectByAcc(acc)==null){ /** * 2.獲取使用者選擇的房間號名稱,如果使用者型別選擇的是現任租客,則不能選擇已入住的房間。 */ //獲取option的value值 String houseId=user.getHouseId(); //通過value值知道房號的物件,獲得名稱值 House house=houseBiz.selectByHouseId(houseId); String houseIdName = house.getHouseId(); user.setHouseId(houseIdName); //獲取使用者選擇的房間號名稱 if(user.getUserClass()==3){ //先判斷搬出時間是否為空 if (user.getOutTime()==null){ //如果使用者的型別是現任租客,則獲取使用者選擇的房間號,在資料庫查詢房間號是否已入住 if (houseBiz.searchByBookerId(user.getHouseId())!=null){ //1.新增使用者 userService.add(user); //2.修改房間入住的狀態 User user1 = userService.selectByAcc(user.getAcc()); house.setBookerId(user1.getId()); houseBiz.editTheBookerId(house); modelAndView.setViewName("redirect:listAll"); return modelAndView; }else{ modelAndView.setViewName("redirect:to_add"); modelAndView.addObject("error1","該房間已被入住!"); return modelAndView; } }else{ //搬出時間必須為空 modelAndView.setViewName("redirect:to_add"); modelAndView.addObject("error1","現任租客的搬出時間必須為空!"); return modelAndView; } }else if(user.getUserClass()==4){ //如果新增的是前任租客,則搬出時間不能為空 if (user.getRuzhuTime()!=null){ userService.add(user); modelAndView.setViewName("redirect:listAll"); return modelAndView; }else{ //搬出時間不能為空 modelAndView.setViewName("redirect:to_add"); modelAndView.addObject("error1","現任租客的搬出時間不能為空!"); return modelAndView; } }else if (user.getUserClass()==1){ //如果新增的是超級管理員,則暫時不開通此功能 modelAndView.setViewName("redirect:to_add"); modelAndView.addObject("error1","暫時不能新增超級管理員使用者"); return modelAndView; } else if (user.getUserClass()==2){ //如果新增的是房間管理員,則房間號為“無” user.setHouseId("無"); user.setOutTime(null); userService.add(user); modelAndView.setViewName("redirect:listAll"); return modelAndView; } modelAndView.setViewName("redirect:listAll"); return modelAndView; }else { modelAndView.setViewName("redirect:to_add"); modelAndView.addObject("error1","該賬號已存在!"); return modelAndView; } }else{ modelAndView.setViewName("redirect:to_add"); modelAndView.addObject("error1","所有帶*號的選項都要選擇!"); return modelAndView; } }
如果你的業務邏輯正確,sql語句沒寫錯,基本上點選新增按鈕就直接新增成功,並且返回使用者列表了。
二、修改功能
修改功能跟新增類似,無非就是業務處理不同,這裡就不詳細說了,修改的時候需要知道資料的id號,我的話是用使用者的賬號為標識,因為賬號是唯一的。
<a href="/user/to_update?acc=${list.acc}" id="update_a">修改 </a>
因為服務端接收要acc這個引數,則在控制器對應的路由新增params為acc
@RequestMapping(value = "/to_update",params = "acc") public String toUpdate(String acc,Map<String,Object> map){ map.put("hlist",houseBiz.selectAllHouse()); map.put("user",userService.selectByAcc(acc)); map.put("clist",userClassBiz.selectAll()); return "users_update"; }
因為獲取到賬號了,則在修改的路由方法裡獲取這個賬號的資訊,並對接收過來的使用者提交資訊進行對比進行業務處理就OK啦。
update方法引數寫個user物件,就能獲取前端提交過來的資料了:
@RequestMapping(value = "/update") public ModelAndView update(User user){
.....省略.......