使用Struts2 spring hibernate框架开发项目---------验证用户名是否可用
使用三大框架开发项目中,当我们注册用户时,往往要求数据库中的用户名是唯一的,所以我们需要在注册时进行用户名验证操作,该操作是如何实现的呢?下面的案例详细介绍此内容。
案例:
Insert.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'inseret.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.6.js"></script> <script type="text/javascript"> function checkUserName(){ var name=$("#uname").val(); $.ajax({ type:"post", url:"csdn/user_checkUserName", data:"user.name="+name, success:function(data){ if(data.user==null){ $("#checkname").html("<font color='red'>用户名可用</font>"); }else{ $("#checkname").html("<font color='red'>用户名不可用</font>"); } }, error:function(jsonmsg){ }, dataType:"json" }); } </script> </head> <body> <h2> 用户登录 </h2> <s:form action="user_insert.action" namespace="/csdn"> <s:textfield label="用户名" name="user.name" id="uname" ></s:textfield> <span id="checkname"></span> <input type="button" onclick="checkUserName()" value="验证用户名"/> <s:textfield label="密码" name="user.pass"></s:textfield> <s:submit value="添加"></s:submit> </s:form> </body> </html>
解析:为了验证用户名是否可用,我在此添加了一个按钮来验证用户名是否可用。该按钮将触发onclick="checkUserName()"事件,采用了Jquery 的ajax请求处理,从页面的文本框中获取你将要注册的用户名,点击验证用户名按钮进行验证,使用ajax的post请求将用户名传到action中执行checkUserName()方法,以下将是根据用户名到底层实现类中进行查找的过程。
Action.Java public class UserAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private UserServiceImpl userServiceImpl; public UserServiceImpl getUserServiceImpl() { return userServiceImpl; } /* 依赖注入方式 */ public void setUserServiceImpl(UserServiceImpl userServiceImpl) { this.userServiceImpl = userServiceImpl; } private List<User> users; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } /*检查用户名是否可用*/ public String checkUserName(){ user=userServiceImpl.findByName(user.getName()); return "checkUserName"; } } UserServiceImpl.java public class UserServiceImpl implements UserService { private UserDaoImpl userDaoImpl; /*依赖注入*/ public void setUserDaoImpl(UserDaoImpl userDaoImpl) { this.userDaoImpl = userDaoImpl; } public User findByName(String name) { return userDaoImpl.findByName(name); } } UserDaoImpl.java public class UserDaoImpl extends HibernateDaoSupport implements UserDao { /* 根据用户名查询用户信息 */ public User findByName(String name) { List list = getHibernateTemplate().find("from User where name=? ", name); if (list.size() > 0) { User user = (User) list.get(0); return user; } return null; } }
Struts-user.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="dxl" extends="json-default" namespace="/csdn"> <global-results> <result name="input">/index.jsp</result> </global-results> <action name="user_*" class="userAction" method="{1}"> <!-- 返回的是一个user对象 user对象(id,name,pass) --> <result name="checkUserName" type="json"> <!-- param参数的配置,includeProperties --> <param name="includeProperties"> user\.id,user\.name </param> </result> </action> </struts>
解析:此文件中使用了通配符,返回的是一个json类型, includeProperties 参数:输出结果中需要包含的属性值,这里正则表达式和属性名匹配,可以用“,”分割填充多个正则表达式。