在项目的开发过程中离不开用户名唯一的验证或者邮件唯一的验证.那通过struts2技术是怎么实现,下面以用户名唯一验证案例讲解。
实现效果:
当用户名输入框失去焦点的时候,能够实现用户名唯一的验证
步骤:
1、设计界面代码
并且引入js文件
[html]view plaincopyprint?
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@ taglib uri="/struts-tags" prefix="s" %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12
13 <title>My JSP 'index.jsp' starting page</title>
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22 <script type="text/javascript" src="${pageContext.request.contextPath }/js/utils.js"></script>
23 <script type="text/javascript" src="${pageContext.request.contextPath }/js/regUser.js"></script>
24 </head>
25
26 <body>
27 <div align="center">
28 <h3>注册页面</h3>
29 <s:form action="regUser" namespace="/csdn" theme="simple">
30 用户名:<s:textfield name="username" id="uname"/><span id="cname"></span>
31 <br>
32 密码:<s:password name="userpass" id="ipass" />
33 <br>
34 邮箱:<s:textfield name="useremail" id="uemail" />
35 <br>
36 <s:submit value="注册"/>
37
38 </s:form>
39 </div>
40 </body>
41 </html>
2、在util.js文件中封装
1、通过id获取dom对象的方法
2、创建XMLHTTPRequest对象的方法
regUser.js
[html]view plaincopyprint?
42 window.onload = function() {
43 var unameDom=$("uname");
44 unameDom.onblur=function(){
45 var content="name="+unameDom.value;
46 var url="./csdn/UserAction_checkName.action?time="+new Date().getTime();
47
48 var xhr=createXHR();
49 xhr.onreadystatechange=function(){
50 if(xhr.readyState==4){
51 if(xhr.status==200||xhr.status==304){
52 $("cname").innerHTML=xhr.responseText;
53 }
54 }
55
56 }
57 xhr.open("POST",url,true);
58 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
59 xhr.send(content);
60 }
61 }
utils.js
[html]view plaincopyprint?
62 function $(id){
63 return document.getElementById(id);
64 }
65 function createXHR(){
66 var xhr;
67 var aVersion=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
68 try{
69
70 xhr=new XMLHttpRequest();
71 }catch(ex){
72 for(var i=0;i<aVersion.length;i++){
73 try{
74 xmlHttpRequest=new ActiveXObject(aVersion[i]);
75 return xmlHttpRequest;
76 }catch(exx){
77 continue;
78 }
79 }
80 }
81 return xhr;
82
83 }
3、创建Action
说明:1、封装的name属性是接受 ajax请求传递的name参数
2、checkName方法就是处理用户名唯一验证的方法. 此方法是在struts.xml文件中通过通配符配置的(详见struts.xml文件)。
[html]view plaincopyprint?
84 package www.csdn.ajax_struts2.action;
85
86 import java.io.IOException;
87 import java.io.PrintWriter;
88
89 import javax.servlet.http.HttpServletResponse;
90
91 import org.apache.struts2.ServletActionContext;
92
93 import www.csdn.ajax_struts2.dao.UserDao;
94 import www.csdn.ajax_struts2.dao.UserDaoImpl;
95
96 public class UserAction {
97 private String name;
98 private UserDao ud=new UserDaoImpl();
99 public String getName() {
100 return name;
101 }
102
103 public void setName(String name) {
104 this.name = name;
105 }
106
107 public String checkName(){
108 HttpServletResponse response=ServletActionContext.getResponse();
109 response.setContentType("text/html;charset=utf-8");
110 PrintWriter out=null;
111 try {
112 out=response.getWriter();
113 } catch (IOException e) {
114 e.printStackTrace();
115 }
116 boolean flag=ud.checkName(name);
117
118 if(flag){
119 out.print("用户名已存在");
120 }else{
121 out.print("用户名可以使用");
122 }
123 out.flush();
124 out.close();
125 return "reg";
126 }
127 }
4、配置struts.xml文件
[html]view plaincopyprint?
128 <?xml version="1.0" encoding="UTF-8"?>
129 <!DOCTYPE struts PUBLIC
130 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
131 "http://struts.apache.org/dtds/struts-2.3.dtd">
132 <struts>
133 <!-- 配置常量 -->
134 <include file="struts_constant.xml"/>
135 <package name="test" namespace="/csdn" extends="struts-default">
136 <action name="UserAction_*" class="www.csdn.ajax_struts2.action.UserAction" method="{1}">
137 <result name="reg">
138 <param name="location">/sc.jsp</param>
139 <param name="charset">utf-8</param>
140 </result>
141 </action>
142 </package>
143
144 </struts>
5、数据库连接部分
[html]view plaincopyprint?
145 public boolean checkName(String username) {
146 boolean flag = false;
147 User user=null;
148 try {
149 Query query = getSession().createQuery(
150 "from User u where u.name=:username").setParameter(
151 "username", username);
152 user=(User) query.uniqueResult();
153 if(user!=null){
154 flag = true;
155 }
156 } catch (Exception e) {
157 throw new RuntimeException(e);
158 }
159 return flag;
160 }