ajax+struts2实现总结
生活随笔
收集整理的這篇文章主要介紹了
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>
??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的特性轉載于:https://www.cnblogs.com/mecca/p/3510596.html
總結
以上是生活随笔為你收集整理的ajax+struts2实现总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#读取Excel 2003/2007的
- 下一篇: LINQ字符拼接的AND和OR操作