首先要注意完成跳轉須要寫完前臺跳轉後要寫後臺controller。
sql
好比<form action="index" method="POST">這個寫完後 後臺得寫數據庫
@RequestMapping(value = "/index", method = RequestMethod.POST,params="back")app
public String back(UserBean userBean,Model model) {post
return "index";spa
} orm
相似這樣的錯誤別再範了。get
首先登陸界面:
input
<div>it
<h3>登陸信息</h3>io
<form action="init1" th:object="${userBean}" method="post">
<div>
登陸ID: <span><input name="userId" type="text" /></span>
</div>
<div>
密碼: <span><input name="userPass" type="password" /></span>
</div>
<div>
<button type="submit" name="search" >登陸</button>
</div>
<div>
<button type="submit" name="register" >註冊</button>
</div>
</form>
</div>
登陸以後能夠完成輸入賬號密碼來登陸,也可註冊。
首先註冊頁面以下:
<h3>註冊信息</h3>
<form action="addUser" th:object="${userBean}" method="post">
<div>
<span><label>ID<span style="display:inline;color:red;">*</span></label></span> <span><input name="userId" type="text" /></span>
</div>
<div>
<span><label>密碼<span style="display:inline;color:red;">*</span></label></span> <span><input name="userPass" type="password" /></span>
</div>
<div>
<span><label>肯定密碼<span style="display:inline;color:red;">*</span></label></span> <span><input name="userPass2" type="password" /></span>
</div>
<div>
<span><label>用戶名<span style="display:inline;color:red;">*</span></label></span> <span><input name="userName" type="text" /></span>
</div>
<div>
<span><input type="submit" name="addUser" value="肯定" /></span>
</div>
<span th:text="${message}"></span>
</form>
註冊就是對數據庫進行插入。
在service裏這麼寫:
public int insert(UserBean frm){
int result=0;
try {
result=updateDao.execute("user.insertuser", frm);
}catch(Exception e){
return result;
}
return result;
最後在controller裏面這麼寫:
@RequestMapping(value = "/addUser", method = RequestMethod.POST,params="addUser")
public String initadd(UserBean userinsert, Model model){
String pass1=userinsert.getUserPass();
String pass2=userinsert.getUserPass2();
if(pass1.equals(pass2)){
int insert = helloWorldService.insert(userinsert);
UserBean user=new UserBean();
model.addAttribute("userBean",user);
if(insert==1){
model.addAttribute("message","註冊成功!");
return "back";
}
else{
model.addAttribute("message","註冊失敗!");
return "back";
}
}else{
model.addAttribute("message","註冊失敗!");
return "back";
}
其中的equals語句是用來判斷兩次密碼是否輸入一致的,須要在userBean中調用兩個密碼的get方法,也就是創建兩個來完成。
而後是登陸。
登陸就是先搜索一次數據庫是否有輸入的賬號密碼。
代碼是:
@RequestMapping(value = "/init1", method = RequestMethod.POST,params="search")
public String initLogin(UserBean userBean, Model model) {
UserBean result = helloWorldService.searchUser(userBean);
List<UserBean>result1 =helloWorldService.searchUserall(userBean);
if(result != null) {
model.addAttribute("List", result1);
return "search";
}else{
model.addAttribute("message", "登陸失敗!");
return "index";
第一步搜索完老師讓完成搜索所有用戶信息上面代碼完成。
第一次select的Spl文是:
<select id="selectuser"
parameterClass="cn.training.bean.UserBean"
resultClass="cn.training.bean.UserBean">
SELECT
user_id as userId,
userName as userName
FROM
user
WHERE
user_id = #userId#
AND userPass = #userPass#
</select>
第二次全檢索的SQL文是:
<select id="selectuserall"
parameterClass="cn.training.bean.UserBean"
resultClass="cn.training.bean.UserBean">
SELECT
user_id as userId,
userName as userName
FROM
user
</select>
接下來是刪除:
首先sql文是:
<delete id="deleteuser"
parameterClass="cn.training.bean.UserBean">
delete from user
WHERE
user_id = #userId#
</delete>
其次須要在search頁面弄一個超連接點了以後刪除,那麼就要在search頁面上寫入下代碼:
<div th:each="userInfo,status:${List}">
<form action="delete" th:object="${userBean}" method="POST">
<table>
<tr>
<td><a th:href="@{delete?(userId=${userInfo.userId})}">刪不刪?<span th:text="${userInfo.userId}">250</span></a></td>
<td><span th:text="${userInfo.userName}"></span></td>
</tr>
</table>
</form>
</div>
其中使用了List方法。
同時在controller中須要寫入:
int result = helloWorldService.deleteUser(userBean);
同時咱們弄了一個確認刪除,取消刪除的頁面
代碼入下:
<form action="Suredelete" method="POST">
<input name="userId" type="hidden" th:value="${userBean.userId}"/>
<button type="submit" name="delete" >確認刪除</button>
<button type="submit" name="cancle" >取消刪除</button>
</form>
然後咱們要讓這兩個button有用的話,咱們須要在controller裏面寫入:
@RequestMapping(value = "/Suredelete", method = RequestMethod.POST,params="delete")
public String suredelete(UserBean userBean, Model model) {
int result = helloWorldService.deleteUser(userBean);
List<UserBean> result1 = helloWorldService.searchUserall(userBean);
model.addAttribute("List", result1);
return "search";
}
@RequestMapping(value = "/Suredelete", method = RequestMethod.POST,params="cancle")
public String deleteno(UserBean userBean, Model model) {
List<UserBean> result1 = helloWorldService.searchUserall(userBean);
model.addAttribute("List", result1);
return "search";
}
大概內容就是這樣。