struts使用Ajax验证用户名是否可用

输入一个数据库里面已经存在的用户名:
这里写图片描述
如果数据库中不存在,则显示可用:
这里写图片描述
使用Ajax来完成此功能。
在上一一篇文章已经完成struts的配置之后,进行该功能的验证。

emp-input.jsp

<!-- 输入lastName的输入框 -->
<s:textfield name="lastName" label="LastName"></s:textfield>

ajax部分代码:

<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" >
        $(function(){
            $(":input[name=lastName]").change(function(){
               //提示name的值已经改变
                alert("name is changed!");
                var name=$(this).val();
                name=$.trim(name);
                var $this = $(this);
                 $this.nextAll("font").remove();
                if(name!=""){
                    //把当前节点后面的所有 font 兄弟节点删除
                    var url = "${pageContext.request.contextPath}/emp-isNameUsedd";
                    var args= {"lastName":name,"time":new Date()};
                    $.post(url,args,function(data){
                    alert("transfer...");
                        if(data == "1"){
                            $this.after("<font color='red'>名字不可用</font>");
                        }else if(data == "0"){
                            $this.after("<font color='green'>名字可用</font>");
                        }else{
                            alert("系统错误,请刷新,重试!");
                        }                   
                    });
                    }else{
                        alert("lastName 不能为空");
                        $(this).val("");
                        $this.focus();
                    }                
            });
        });
    </script>

先关的action配置:
struts.xml配置:

<action name="emp-*" class="employeeAction" method="{1}">
        <result name="list">/WEB-INF/views/emp-list.jsp</result>
        <!-- struts的ajax配置 -->
        <result type="stream" name="ajax-success">
              <param name="contentType">text/html</param>
              <param name="inputName">inputStream</param>
        </result>   
        <result name="INPUT">/WEB-INF/views/emp-input.jsp</result>
        <result name="success" type="redirect">/emp-list</result>
    </action>

Action部分:

/**
     * 验证用户名是否已经被使用
     */
    private String lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String isNameUsedd(){
        //如果改名字被使用,返回1
        boolean flg = employeeService.isNameUsed(lastName);
        if(flg){
            try {
                inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else {
            try {
                inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return "ajax-success";
    }

service部分:

public boolean isNameUsed(String lastName) {
        Employee employee = employeeDao.getByName(lastName) ;
        //System.out.println("EmployeeService.isNameUsed..."+lastName);
        if (employee != null) {
            return true;
        }else {
            System.out.println("employeeService return false....");
            return false;
        }
    }

dao部分:

public Employee getByName(String lastName) {
        System.out.println("EmployeeDao.getByName..."+lastName);
        String hql = "from Employee e where e.lastName=?";
        Query query = getSession().createQuery(hql).setString(0, lastName);
        Employee employee = (Employee) query.uniqueResult();
        if (employee!=null) {
            return employee;
        }else {
            System.out.println("EmployeeDao...return null");
            return null;
        }
    }

总结

  1. 当lastName的内容改变的时候,使用jQuery获得该标签内容
  2. 并使用$.post(url,args,function(data){...})传送到actionisNameUsedd方法。
  3. action通过service和dao层的查询,判断该用户是否存在
  4. 如果存在使用struts的ajax方式,利用inputStream = new ByteArrayInputStream("[自定义标志位]".getBytes("UTF-8"));返回
  5. 返回到emp-input.jsp界面$.post(url,args,function(data){...})内判断用户是否存在,并在lastName旁边显示”该用户是[否]存在”
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值