• ajax+struts2实现总结


    ajax+struts2验证用户名是否存在
    首先需要从页面通过document.getElementById或name获得输入的内容,传到struts2的中,这时就需要struts.xml文件起到作用了,也是重要的一步,首先需要添加lib:json-lib-2.4-jdk15.jar,struts2-json-plugin-2.3.15.2.jar,并将返回类型设置type="json",而且添加了param excludeNullProperties声明,

      excludeNullProperties 参数:表示是不是去掉空值, 默认值是false,如果设置为true会自动将为空的值过滤,只输出不为空的值。 
     
      
    <result type="json"><param name="excludeNullProperties">true</param></result>
     
    要是只想取出list对象集合中,对象的某几个属性,则需要下列配置:
    <action name="recommendList" class="VideoAction" method="recommendList">
    <result name="success" type="json">
    <param name="includeProperties">recommendList[d+].videoTime,recommendList[d+].videoId,recommendList[d+].videoImg,recommendList[d+].videoTitle </param>
    </result>
    </action>
    之后进入action中,则需要注意到一个新的问题,就是setter和getter,他们会自动转换为HttpServletRequest的getParameter()和setAttribute(),也就是说在不需要将参数作为返回值返回到View层时,是可以不写getter方法的,这也是在我实现ajax+struts2过程中,总是显示其他不想要的内容的原因。
    然后就会返回到javascript中,这时,因为定义了json的type,所以在javascript中的对象为json的object类型,需要使用for in循环将json串中的内容遍历出来,找到想要的内容,还有一个hasOwnProperty()的方法,他是用来判断返回的json中是否有json对象的,其实完全可以不是用,可能这样会降低代码的健壮性吧。
    还有一点就是javascript的变量作用域的问题,如下代码:
      $("#acount" ).blur( function(){  
            var text = inputUserNameObj.val();  
            $.post("ajaxCheck.action?acount=" +text, null, function(result){           
               for( var i in result){ 
                  if(result.hasOwnProperty(i)){ 
                      alert(i+"----"+result[i]) ;
                  }
                  
              }
              alert( "js中result=" +result[i]);
               if(result[i]!=( '可以使用' )){
                 document.getElementById( "namemessage" ).innerHTML="<font color='red'>"+result[i]+ "</font>";
                } else{
                 document.getElementById( "namemessage" ).innerHTML="<font color='green'>"+result[i]+ "</font>";
                }
            });  
        });
     难道for in里边的变量i能够带到外边的if中?这在java中是不可以的吧?还搞不太明白
    修改——————————
    上边所说的变量作用域问题其实是javascript函数的一个闭包函数,是javascript的特性
  • 相关阅读:
    leetcode-409
    leetcode-836
    leetcode-1160
    leetcode-面试题13
    leetcode-695
    go的一些小lib
    leetcode-300
    cookie
    php上传文件
    PHP 文件创建/写入
  • 原文地址:https://www.cnblogs.com/mecca/p/3510596.html
Copyright © 2020-2023  润新知