register.jsp <%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title> 注册 </title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- 这事前端验证,后台验证在Regiseter.action中写 --> <script type="text/javascript"> function checkUser() { /* 第一个表单第一个对象-username */ var username = document.forms[0].elements[0].value; var password = document.forms[0].elements[1].value; //判断用户名为能为空 if(username=="") { alert("用户名不能为空"); return false; } //判断用户名第一位不能为数字 if(!isNaN(username.charAt(0))) { alert("用户名第一个字符不能为数字"); return false; } //判断用户名的长度不能小于4位 if(username.length < 4) { alert("用户名不能少于4个字符"); return false; } } //判断密码为能为空 if(password=="") { alert("密码不能为空"); return false; } //判断密码的长度不能小于6位 if(password.length < 6) { alert("密码长度不能小于6位"); return false; } </script> </head> <body> <s:property value="message"/> <a href="index.jsp">返回登录</a> <hr/> 请您注册: <!-- 以前action名是Register:加了post --> <form action="Register.action" onsubmit="return checkUser()" method="post"> 用户名: <input type="text" name="username" /> 密码: <input type="password" name="password"/> <input type="submit" value="提交"/> </form> <!-- 测试 --> <%=request.getParameter("username") %> </body> </html> RegisterAction.java package com.mys.action; import java.util.List; import org.apache.commons.lang.StringUtils;//调用了该包的StringUtils类:验证用户名和密码 import org.hibernate.Query; import org.hibernate.Session; import com.mys.dao.UserDAO; import com.mys.domain.*; import com.mys.util.HibernateUtil; import com.dao.impl.*; /** * 还差个验证文件:最好添个邮箱,以及重复输入密码的功能 * @author Administrator * */ public class RegisterAction { private String username; private String password; private String confirmpassword; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getConfirmpassword() { return confirmpassword; } public void setConfirmpassword(String confirmpassword) { this.confirmpassword = confirmpassword; } UserDAO userDAO=new UserDAOHibernate(); public String execute() throws Exception{ User user=new User(); user.setUsername(username); user.setPassword(password); UserDAOHibernate h=new UserDAOHibernate(); ////这个if语句只能判断数据库中是否有该用户 if(userDAO.findByName(username)>0){ message="你好,用户名:"+username+"已经注册,请重新选择用户名!"; return "input"; } //这个语句是提交事务,录入数据的:实在不行暂时不管 else if(userDAO.save(user)==null){ if(user.getUsername()==null || "".equals(user.getUsername())){ message="用户名不能为空!"; return "input"; } if(user.getPassword()==null || "".equals(user.getPassword())){ message="密码不能为空!"; return "input"; } } 具体实现 package com.dao.impl; /** * 实现登陆&注册验证 */ import java.io.File; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Query; import org.hibernate.Transaction; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.mys.dao.UserDAO; import com.mys.domain.User; import com.mys.util.HibernateUtil; public class UserDAOHibernate implements UserDAO{ private static final Log log = LogFactory.getLog(UserDAOHibernate.class); private static SessionFactory sessionFactory = null; private Transaction tx; private Session session; static { Configuration configuration = new Configuration(); configuration.configure(); sessionFactory = configuration.buildSessionFactory(); } /*//以前是下边这样 SessionFactory sessionFactory=new Configuration().configure (new File("D:\ProgramFiles\WebProWorkPace\LRtest\src\Hibernate.cfg.xml")) .buildSessionFactory();*/ //登陆验证 public boolean isValidUser(String username, String password) { log.debug("in UserDAO IsValid"); try { Session session = sessionFactory.openSession(); String hql = "from User as u where u.username=? and u.password=?"; Query query = session.createQuery(hql); query.setString(0, username); query.setString(1, password); List userList = query.list(); if (userList.size() > 0) { session.close(); return true; } session.close(); return false; } catch (RuntimeException e) { log.error("isValidUser failed", e); throw e; } } //注册验证! public User save(User transientInstance) { log.debug("saving user instance"); Session session = sessionFactory.openSession(); Transaction tran = session.beginTransaction(); /*已有该用户*/ try { if (findByName(transientInstance.getUsername()) > 0) { return null; } /* 没有该用户,录入数据库*/ session.save(transientInstance); session.flush(); tran.commit(); log.debug("save successful"); session.close(); return transientInstance; } catch (RuntimeException e) { tran.rollback(); log.error("save failed", e); throw e; } } //注册验证 public int findByName(String username) { Session session = sessionFactory.openSession(); String hql = "from User as u where u.username=?"; Query query = session.createQuery(hql); query.setParameter(0, username); List userlist = query.list(); if (userlist.size() > 0) { return 1; } return 0; } } |
|
后端没有写得正确,导致用户名为空或密码为空都可以录入数据。求教大神应该怎样改才能阻止录入其中之一为空的数据录入?
|
|
2分 |
给username的input加个ID
if($(“”#username””).val() == “” ){ alert(“”用户名不能为空!””); return; } |
2分 |
if(!password)就可以
|
你这个是前台的验证啊,我想验证后台,action里的没写对 |
|
2分 |
得使用ajax,异步去后台请求验证
参考 http://blog.csdn.net/magi1201/article/details/44569657 用户名是否重复校验 |
14分 |
if(userDAO.findByName(username)>0){ message="你好,用户名:"+username+"已经注册,请重新选择用户名!"; return "input"; } //这个语句是提交事务,录入数据的:实在不行暂时不管 else if(userDAO.save(user)==null){ if(user.getUsername()==null || "".equals(user.getUsername())){ message="用户名不能为空!"; return "input"; } if(user.getPassword()==null || "".equals(user.getPassword())){ message="密码不能为空!"; return "input"; } } 这个逻辑都有点问题啊!你在else if 的判断中已经执行了save操作了! if(user.getUsername()==null || "".equals(user.getUsername())){ message="用户名不能为空!"; return "input"; } if(user.getPassword()==null || "".equals(user.getPassword())){ message="密码不能为空!"; return "input"; } if(userDAO.findByName(username)>0){ message="你好,用户名:"+username+"已经注册,请重新选择用户名!"; return "input"; } //这个语句是提交事务,录入数据的:实在不行暂时不管 else if(userDAO.save(user)==null){ return "input"; } return "success"; |