新聞中心
Web2.0的隨波逐流,Ajax那是大放異彩,Struts2框架自己整合了對(duì)Ajax的原生支持(struts 2.1.7+,之前的版本可以通過插件實(shí)現(xiàn)),框架的整合只是使得JSON的創(chuàng)建變得異常簡(jiǎn)單,并且可以簡(jiǎn)單的融入到Struts2框架中,當(dāng)然這只是在我們需要JSON的時(shí)候才會(huì)顯得流光溢彩。

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供梁山網(wǎng)站建設(shè)、梁山做網(wǎng)站、梁山網(wǎng)站設(shè)計(jì)、梁山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、梁山企業(yè)網(wǎng)站模板建站服務(wù),十余年梁山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
首先不談Struts2的原生支持,我們自己寫一個(gè)ajax示例,使用異步請(qǐng)求,直接請(qǐng)求action動(dòng)作:
InfoAction.java
- packagecn.codeplus.action;importcom.opensymphony.xwork2.ActionSupport;
- publicclassInfoAction extendsActionSupport {
- privatestaticfinallongserialVersionUID =1359090410097337654L;
- publicString loadInfo() {returnSUCCESS;
- }
- }
InfoAction僅僅是簡(jiǎn)單的返回"success"。
index.jsp
"> 獲取
index.jsp包含一個(gè)按鈕,點(diǎn)擊按鈕則會(huì)觸發(fā)異步請(qǐng)求事件。
struts.xml
/info.jsp
可見上面的異步請(qǐng)求的結(jié)果將會(huì)是加載info.jsp,info.jsp只是一個(gè)簡(jiǎn)單網(wǎng)頁,不列出了。
運(yùn)行效果如下:
單擊獲取之后:
此時(shí)的頁面源代碼:
以上說的異步請(qǐng)求僅適用于請(qǐng)求單個(gè)文件,如果我們請(qǐng)求的是動(dòng)態(tài)數(shù)據(jù),并且數(shù)據(jù)需要以JSON格式返回,上面的方法將會(huì)顯得力不從心,這是struts2的原生支持就得出馬了。
使用struts2的ajax,必須在項(xiàng)目中引入struts2-json-plugin-2.2.1.jar,在版本2.1.7+都一句綁定在struts2發(fā)行包里面了(之前的版本可以在這下載)。記住,要引入struts2-json-plugin-2.2.1.jar。
這次我們使用另一個(gè)例子,模擬加載評(píng)論:
dto對(duì)象,Comment.java
- packagecn.codeplus.po;
- publicclassComment {
- privatelongid;privateString nickname;
- privateString content;publiclonggetId() {returnid;
- }
- publicvoidsetId(longid) {
- this.id =id;
- }
- publicString getNickname() {returnnickname;
- }
- publicvoidsetNickname(String nickname) {
- this.nickname =nickname;
- }
- publicString getContent() {returncontent;
- }
- publicvoidsetContent(String content) {
- this.content =content;
- }
- }
新的InfoAction.java
- packagecn.codeplus.action;
- importjava.util.ArrayList;importjava.util.List;
- importcn.codeplus.po.Comment;
- importcom.opensymphony.xwork2.ActionSupport;
- publicclassInfoAction extendsActionSupport {
- privatestaticfinallongserialVersionUID =1359090410097337654L;
- privateList
comments =newArrayList ();//沒getter and setter方法的屬性不會(huì)被串行化到JSON - @SuppressWarnings("unused")
- privateString title;//?。?!使用transient修飾的屬性也會(huì)被串行化到JSONprivatetransientString content;publicString loadInfo() {
- title="123木頭人";
- content="你是木頭人,哈哈。";
- loadComments();returnSUCCESS;
- }/*** 加載留言信息*/
- privatevoidloadComments() {
- Comment com1 =newComment();
- com1.setContent("很不錯(cuò)嘛");
- com1.setId(1);
- com1.setNickname("納尼");
- Comment com2 =newComment();
- com2.setContent("喲西喲西");
- com2.setId(2);
- com2.setNickname("小強(qiáng)");
- comments.add(com1);
- comments.add(com2);
- }publicList
getComments() {returncomments; - }publicvoidsetComments(List
comments) {this.comments =comments; - }publicstaticlonggetSerialversionuid() {returnserialVersionUID;
- }publicString getContent() {returncontent;
- }publicvoidsetContent(String content) {this.content =content;
- }
- }
- index.jsp還是那個(gè)index.jsp。(*^__^*) 嘻嘻……
- struts.xml變化挺大:
在struts.xml中:
首先,package extends由struts-default轉(zhuǎn)變?yōu)閖son-default,這是必須的,只用在json-default中才包含下面使用的result type為 json。
然后就是result類型需顯示指明為json,result標(biāo)簽內(nèi),無需指明視圖指向的界面。
***就是運(yùn)行結(jié)果啦:
點(diǎn)擊“獲取”按鈕之后:
可見comments對(duì)象和content對(duì)象都被串行化到JSON數(shù)據(jù)了,不知道是不是版本的問題,很多資料都說使用transient修飾的屬性不會(huì)被串行化到JSON的。
為了使content對(duì)象不被串行化到JSON,在不能舍棄其getter setter方法的時(shí)候,我們可以這樣在content的getter方法上面加上注解:@JSON(serialize=false)
- ...
- @JSON(serialize=false)publicString getContent() {returncontent;
- }publicvoidsetContent(String content) {this.content =content;
- }
- ...
這時(shí)的結(jié)果如下:
@JSON和json類型的result都還有很多可選項(xiàng),無非就是串行化誰,不串行化誰,返回?cái)?shù)據(jù)的MIME類型,讀者可以自行參考相關(guān)文檔。
獲取到JSON數(shù)據(jù)了,下一步就是在前臺(tái)使用js處理JSON數(shù)據(jù)了,本人JS不精,喜歡使用jQuery解析,如有興趣,且聽下回分解jQuery解析JSON數(shù)據(jù)。
文章標(biāo)題:初析Struts2中的Ajax開發(fā)實(shí)例
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/ccdchjg.html


咨詢
建站咨詢
