SSH_框架整合5--验证用户名是否可用

SSH_框架整合5--验证用户名是否可用

1 emp-input.jsp中编写ajax验证用户名是否可用: 

 <script type="text/javascript" SRC="scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$(":input[name=lastName]").change(function(){
var val = $(this).val();
val = $.trim(val);
var $this = $(this); if(val != ""){
//把当前节点后面的所有 font 兄弟节点删除
$this.nextAll("font").remove(); var url="emp-validateLastName";
var args={"lastName":val,"time":new Date()};
$.post(url,args,function(data){
//姓名可用
if(data == "1"){
$this.after("<font color='green'>LastName可用!</font>");
}
//不可用
else if(data == "0"){
$this.after("<font color='red'>LastName不可用!</font>");
}
//服务器错误
else{
alert("服务器错误!");
}
});
}else{
alert("lastName不能为空!");
$(this).val("");
//$(this).focus();
}
});
})
</script>

2  完善类:底层编写代码实现验证的方法:

  (1)EmployeeDao.java

 //4 校验注册的姓名是否可用
public Employee getEmployeeByLastName(String lastName){
String hql = "FROM Employee e WHERE e.lastName = ?";
Query query = getSession().createQuery(hql).setString(0, lastName);
Employee employee = (Employee) query.uniqueResult();
//System.out.println("***"+employee.getDepartment().getClass().getName());
return employee;
}

  (2)EmployeeService.java

 //4 校验注册的姓名是否可用
public boolean lastNameIsValid(String lastName){
//在数据库中查完结果为null,说明没有被注册过
return employeeDao.getEmployeeByLastName(lastName) == null;
}

  (3)EmployeeAction.java  

 //5 校验注册的姓名是否可用
private String lastName; public void setLastName(String lastName) {
this.lastName = lastName;
} public String validateLastName() throws UnsupportedEncodingException{
if(employeeService.lastNameIsValid(lastName)){
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
}else{
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
}
return "ajax-success";
}

  其中delete()方法的return修改为:return "ajax-success";

3 applicationContext.xml中修改为只读:

  <tx:method name="lastNameIsValid" read-only="true"/>

上一篇:SSH_框架整合2—查询显示


下一篇:【Java并发系列03】ThreadLocal详解