点击“增加用户”链接选项,进入添加用户界面,
根据要求填写用户信息可添加新用户,系统会对输入数据的合法性进行验证。这里对登录帐号的验证采用Ajax 技术实现:当操作员填写完帐号并将光标移出文本框时,系统会自动查询数据库,并判断该帐号是否已经被占用,如果被占用,就会给出“该帐号已经被占用”的提示。具体实现如下:
注册页面addUser.jsp 中表单功能代码为:
<p>
<label>用户帐号:</label>
<input type="text" name="userNo" id="userNo" onblur="checkLoginId()" class="required length-range-0-10 validate-number"/>
(必须由0~9的数字组成)
<font color="red"><div id="suggest" style="display:none"></div></font>
</p>
当光标移出文本框时,就会执行javaScript 文件checkLoginId.js 中的checkLoginId ()方法,其中checkLoginId.js 的代码如下:
var xmlHttp;
function createXmlHttp() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function checkLoginId() {
createXmlHttp();
var userNo = document.getElementById("userNo").value;
xmlHttp.onreadystatechange = showSuggest;
xmlHttp.open("GET", "judgeUserNo.action?userNo="+userNo, true);
xmlHttp.send(null);
}
function showSuggest() {
if (xmlHttp.readyState == 4) {
document.getElementById("suggest").innerHTML = "";
var suggestsText = xmlHttp.responseText;
if(suggestsText!=""){
document.getElementById("suggest").innerHTML +=suggestsText;
document.getElementById("suggest").style.display = "";
}else{
document.getElementById("suggest").style.display = "none";
}
}
}
checkLoginId.js 是该功能Ajax 技术实现的核心:首先执行createXMLHttp() 方法创建XMLHttpRequest 对象,然后向页面struts.xml发送带参数的请求,sturx.xml中的action代码如下
<action name="judgeUserNo" class="org.gss.action.admin.user.JudgeUserNoAction">
<result name="success">
/judgeUserNoSuccess.jsp
</result>
<result name="error">
/judgeUserNoError.jsp
</result>
</action>
JudgeUserNoAction 根据参数对数据库进行查询并返回"success"或"error",
JudgeUserNoAction.java 代码如下:
package org.gss.action.admin.user;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gss.action.factory.GeneralContext;
import org.gss.ejb.userinfo.ent.TGssUserinfo;
import org.gss.ejb.userinfo.service.GssUserinfoService;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author WuShuicheng .
* @version create at : 2009-10-11,05:52:32.<br>
* description : this action use to judge the userNo weather being
* used.
*
*/
public class JudgeUserNoAction extends ActionSupport
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -6922839457261762622L;
/**
* define String userNo user judge it weather used.
*/
private String userNo;
/**
* Defined a parameter to store GeneralContext.
*/
private final GeneralContext linkContext = new GeneralContext();
/**
* Defined a parameter to store TGssRulesService interface.
*/
private final GssUserinfoService gssUserinfoService = linkContext
.getGssUserinfoServiceContext();
/**
* execute method.
*
* @return success.
*/
public final String execute()
{
Log logger = LogFactory.getLog(this.getClass());
TGssUserinfo user = null;
user = gssUserinfoService.getOneUserByUserNo(userNo);
if (user == null)
{
logger.info("the userNo " + userNo + "didn't being used.");
return SUCCESS;
} else
{
logger.info("the userNo " + userNo + "id being used.");
return ERROR;
}
}
/**
* @return the userNo use to judge it weather used.
*/
public final String getUserNo()
{
return userNo;
}
/**
* @param argUserNo
* use to judge it weather used .
*/
public final void setUserNo(final String argUserNo)
{
this.userNo = argUserNo;
}
}
EJB中接口方法为:
/**
* @author WuShuicheng .
* @param userNo
* .
* @return user. this interface use to get the object by
* the userNo.
*/
TGssUserinfo getOneUserByUserNo(String userNo);
会话Bean中的代码如下:
/**
* select head.
*/
private static final String SQLHEADSTRING = "select o from TGssUserinfo o where ";
public final TGssUserinfo getOneUserByUserNo(
final String userNo)
{
TGssUserinfo user = null;
String str = SQLHEADSTRING
+ "o.userNo = :argUserNo";
try
{
Query query = em.createQuery(str);
query.setParameter(argUserNo,
userNo);
user = (TGssUserinfo) query
.getSingleResult();
logger
.info("get one user by userNo :"
+ userNo + "success!");
} catch (Exception e)
{
logger
.info("get one user by userNo :"
+ userNo + "failed!");
e.printStackTrace();
return null;
}
return user;
}
judgeUserNoError.jsp的代码如下:
<%@page contentType="text/html;charset=utf8"%>
<%@ page language="java"%>
<%
out.clear(); //清空当前的输出内容(空格和换行符)
out.println("该帐号已经被使用!");
%>
之后程序调用回调函数showSuggest() ,最终在注册页面显示验证信息。
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛