Java web項目:bootstrap實現注冊頁面,mvc模式聯合mysql數據庫檢查用戶名的唯一性。
一、實現效果:
重置這里有bug,bootstrapValidator驗證不能重置,待解決。
二、代碼準備:
引入bootstrap,bootstrapValidator和jquery。
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/bootstrap.min.css" rel="external nofollow" /> <link rel="stylesheet" href="<%=request.getContextPath() %>/css/bootstrapValidator.min.css" rel="external nofollow" />
<script src="<%=request.getContextPath() %>/js/jquery.min.js"></script> <script src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> <script src="<%=request.getContextPath() %>/js/bootstrapValidator.min.js"></script>
三、部分代碼:
register.jsp注冊部分代碼。
<form id="registerForm" action="<%=request.getContextPath() %>/UserServlet" method="post"> <input type="hidden" name="method" value="register"/> <div class="form-group"> <label>用戶名</label> <input type="text" class="form-control" name="userName" placeholder="用戶名由2-12位字符組成" /> </div> <div class="form-group"> <label>郵箱</label> <input type="text" class="form-control" name="userEmail" placeholder="郵箱" /> </div> <div class="form-group"> <label>密碼</label> <input type="password" class="form-control" name="userPassword" placeholder="密碼由6-10位字母數字組成" /> </div> <div class="form-group"> <label>確認密碼</label> <input type="password" class="form-control" name="confirmUserPassword" placeholder="再次輸入密碼" /> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">注冊</button> <input type="reset" class="btn btn-primary" value="重置"> </div> </form>
利用bootstrapValidator表單驗證代碼。 ajax部分有詳細注釋
<script type="text/javascript"> $(function() { $('#registerForm').bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { userName: { message: 'The username is not valid', validators: { notEmpty: { message: '用戶名不能為空' }, stringLength: { min: 2, max: 12, message: '用戶名由2-12位字符組成' }, threshold: 2,//有2字符以上才發送ajax請求 remote: {//ajax驗證。server result:{"valid",true or false} url: "/ImageShare/UserServlet", message: '用戶名已存在,請重新輸入', delay: 1000,//ajax刷新的時間是1秒一次 type: 'POST', //自定義提交數據,默認值提交當前input value data: function(validator) { return { userName : $("input[name=userName]").val(), method : "checkUserName"http://UserServlet判斷調用方法關鍵字。 }; } } } }, userEmail: { validators: { notEmpty: { message: '郵箱不能為空' }, emailAddress: { message: '輸入不是有效的電子郵件地址' } } }, userPassword: { validators: { notEmpty: { message: '密碼不能為空' }, stringLength: { min: 6, max: 10, message: '密碼由6-10位字符組成' }, identical: { field: 'confirmUserPassword', message: '密碼輸入不一致' } } }, confirmUserPassword: { validators: { notEmpty: { message: '密碼不能為空' }, stringLength: { min: 6, max: 10, message: '密碼由6-10位字符組成' }, identical: { field: 'userPassword', message: '密碼輸入不一致' } } } } }); }); </script>
UserServlet.java檢查用戶名唯一性部分代碼。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); //0、獲取method判斷執行操作 String method = request.getParameter("method"); if ("checkUserName".equals(method)) { //驗證用戶名是否已存在 checkUserName(request,response); } } //根據用戶名稱查詢,檢查用戶名稱的唯一性(用戶注冊) public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setCharacterEncoding("UTF-8"); //返回json數據,格式為{"valid",true} 表示合法,驗證通過。{"valid":false} 表示不合法,驗證不通過 String jsonResult = ""; String userName = request.getParameter("userName"); //去數據進行唯一性確認 if (userName!=null) { //服務層service調用數據庫訪問層dao中的searchUserName方法。 boolean b = UserServiceImpl.searchUserName(userName); if (b) { //如果名稱存在 jsonResult = "{\"valid\":false}"; }else{ //如果該名稱不存在 jsonResult = "{\"valid\":true}"; } } else { jsonResult = "{\"valid\":false}"; } //response把jsonResult打到前臺 response.getWriter().write(jsonResult); }
四、總結:
1.利用bootstrapValidator的ajax表單驗證用戶名已存在關鍵是自定義提交的數據。
2.將當前input的value值和判斷操作方法的method關鍵字提交
3.注意當server必需返回形如:{“valid”,true or false} 的json數據格式
4.servlet通過 response.getWriter().write(jsonResult) 返回響應的內容jsonResult到前臺頁面。
如果大家還想深入學習,可以點擊這里進行學習,再為大家附3個精彩的專題:
Bootstrap學習教程
Bootstrap實戰教程
Bootstrap插件使用教程
以上就是關于本文的全部內容,希望對大家的學習有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。