登入注冊
首先,我們webapp要實作使用者登入,必須得能建立使用者。是以先把注冊使用者放在前面。
預期功能:打開注冊頁面
填寫注冊資訊
點選注冊
顯示注冊後的提示資訊
一個web注冊頁面
web頁面能進行基本的資料效驗
伺服器能存儲使用者的注冊資訊
注冊動作完成後,傳回提示頁面。
一般在開發中,有了大概樣子的功能子產品,需要整理一下業務流程和程式執行流程
大概的流程圖如下所示:
上圖說明:
在web頁面完成注冊資訊的填寫後,需要在web頁面做一些基本的資料效驗。
注冊資訊通過基本的驗證後,直接送出到伺服器,tomact → servelt → spring 。後端程式一切都被spring接管了,是以,需要在
spring
中完成這些功能。
spring
和外界通信,我們都是在
Controller
中完成。是以我們在
Controller
中處理資料。
當資料通過了
Controller
中的校驗後,需要在
Controller
中來查找資料庫是否存在同樣的使用者名,通用的資料操作流程如:Controller → Service → Dao。
Service是為我們的程式提供服務的,盡量每個
Service
對應一個
Dao
,隻需要提供單一的資料驅動,在外層進行業務組裝,這樣就能達到目的,同樣也能将程式
解耦,以後的維護也就相對簡單了。
在實際項目中主要是使用JSON資料,是以要寫一個傳回json資料的實體
在domain包中建立一個ResponseObj類
public class ResponseObj <T>{
public final static int OK = 1, FAILED = 0, EMPUTY = -1;
public final static String OK_STR = "成功", FAILED_STR = "失敗", EMPUTY_STR = "資料為空";
private int code; // 狀态碼,0成功;1空資料;-1請求失敗
private String msg;
private Object data;
省略code,msg,data的set,get方法
}
要寫登入注冊的接口,我們先建立一個mvc目錄,目錄下controller包
在
controller
包中建立一個
MainController
的類。
@Controller
@RequestMapping("/mvc")
public class MainController {
/**
* 登陸頁面
* @return
*/
@GetMapping("/login")
public String login(){
return "login";
}
}
在實際項目中主要是使用JSON資料,是以不使用ModelAndView
JSON資料解析我用的是阿裡巴巴的
Fastjson
具體使用:
http://www.jianshu.com/p/3c45f4be2c90MainController
主要是用于跳轉到登入頁面
我們在Controller目錄下建立一個
UserController
類
@Controller
@RequestMapping("/userAction")
public class UserController {
@Autowired
private UserServiceImpl userService; //自動載入Service對象
private ResponseObj responseObj; //傳回json資料的實體
/**
* @param req http請求
* @param user 發起請求後,spring接收到請求,然後封裝的bean資料
* @throws Exception
*/
@PostMapping("/reg")
@ResponseBody
public Object reg(HttpServletRequest request, User user, HttpSession session) throws Exception {
JSONObject jsonObject=new JSONObject();
responseObj = new ResponseObj<User>();
if (null == user) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("使用者資訊不能為空!");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
if (StringUtils.isEmpty(user.getLoginId()) || StringUtils.isEmpty(user.getPwd())) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("使用者名或密碼不能為空!");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
if (null != userService.findUser(user)) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("使用者已經存在!");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
try {
userService.add(user);
} catch (Exception e) {
e.printStackTrace();
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("其他錯誤!");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
userService.updateLoginSession(request.getSession().getId(), user.getLoginId());
responseObj.setCode(ResponseObj.OK);
responseObj.setMsg("注冊成功");
user.setPwd(session.getId()); //單獨設定密碼為sessionId 誤導黑客,前端通路伺服器的時候必須有這個資訊才能操作
user.setNextUrl(request.getContextPath() + "/mvc/home"); //單獨控制位址
responseObj.setData(user);
session.setAttribute("userInfo", user);
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
/**
* 登入接口
* @param req
* @param user
* @return
*/
@PostMapping("/login")
@ResponseBody
public Object login(HttpServletRequest request, User user,HttpSession session) throws Exception{
JSONObject jsonObject=new JSONObject();
responseObj = new ResponseObj<User>();
if (PublicUtil.isJsonRequest(request)) { //确認是否json送出
user = new GsonUtils().jsonRequest2Bean(request.getInputStream(), User.class); //轉換為指定類型的對象
return user.toString();
}
if (null == user) {
responseObj.setCode(ResponseObj.EMPUTY);
responseObj.setMsg("登入資訊不能為空");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
if (StringUtils.isEmpty(user.getLoginId()) || StringUtils.isEmpty(user.getPwd())) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("使用者名或密碼不能為空");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
//查找使用者
User user1 = userService.findUser(user);
if (null == user1) {
responseObj.setCode(ResponseObj.EMPUTY);
responseObj.setMsg("未找到該使用者");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
} else {
if (user.getPwd().equals(user1.getPwd())) {
user1.setPwd(session.getId());
user1.setNextUrl(request.getContextPath() + "/mvc/home");
responseObj.setCode(ResponseObj.OK); //登入成功,狀态為1
responseObj.setMsg(ResponseObj.OK_STR);
responseObj.setData(user1); //登陸成功後傳回使用者資訊
userService.updateLoginSession(request.getSession().getId(), user.getLoginId());
session.setAttribute("userInfo", user1);
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
} else {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("使用者密碼錯誤");
jsonObject= (JSONObject) JSON.toJSON(responseObj);
return jsonObject;
}
}
}
}
相關的登入注冊頁面我就放在github上吧,還有相關的js css image資源
我在使用過程中發現js,css資源不生效,
提示
Absolute paths not recommended in JSP
也就是
在JSP中不推薦使用絕對路徑
解決方法
在相對路徑前加上
${pageContext.request.contextPath}
這樣JSP取得資源的絕對路徑了
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/jquery-3.1.1.min.js"></script>
這裡可以做前後端分離,用純粹的html+js來調用Api接口實作前後端分離。下一個part寫
action="<%=request.getContextPath()%>/userAction/reg" method="post"
<%=request.getContextPath()%>
這是指向應用的根路徑
mothod
是說明我們請求的方式
Paste_Image.png
如果使用form表單送出時:
form表單中,每個input的name我們需要和後端的接口那邊的字段對應。
當我們的字段對應後,spring可以自動把請求的内容轉換為适應的對象。
存入資料庫的資訊有亂碼
也就是說Form表單送出的時候出現亂碼
spring架構提供的字元集過濾器
**spring Web MVC架構提供了
org.springframework.web.filter.CharacterEncodingFilter
用于解決POST方式造成的中文亂碼問題 **
可以使用過濾器處理亂碼問題
需要在
web.xml
中加入
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
這些是登入注冊的頁面
主要參考于大牛
Clone丶記憶的SSM內建之路