近期自己搞好了系統的添加、修改、條件查詢的功能了,折騰了幾天,主要是添加修改的業務判斷比較多,導致那個負責修改的控制器代碼加註釋都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" onclick="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" onkeyup="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" onclick="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,這個路由
onclick="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){
.....省略.......