Struts2中的Ajax开发方法是什么-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Struts2中的Ajax开发方法是什么

本篇内容介绍了“Struts2中的Ajax开发方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在鹿寨等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、网站建设 网站设计制作按需策划,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销,外贸网站建设,鹿寨网站建设费用合理。

首先不谈Struts2的原生支持,我们自己写一个ajax示例,使用异步请求,直接请求action动作:

InfoAction.java

packagecn.codeplus.action;importcom.opensymphony.xwork2.ActionSupport;  publicclassInfoAction extendsActionSupport {privatestaticfinallongserialVersionUID =1359090410097337654L;  publicString loadInfo() {returnSUCCESS;  }  }

InfoAction仅仅是简单的返回"success"。

index.jsp

  "> 获取    functionloadInfo() {  $("#info").load("loadInfo");  }    
  

index.jsp包含一个按钮,点击按钮则会触发异步请求事件。

struts.xml

  /info.jsp  

可见上面的异步请求的结果将会是加载info.jsp,info.jsp只是一个简单网页,不列出了。

运行效果如下:

Struts2中的Ajax开发方法是什么

单击获取之后:

Struts2中的Ajax开发方法是什么

此时的页面源代码:

Struts2中的Ajax开发方法是什么

标签中嵌套了标签,不符合规范,其实我们只要吧info.jsp写的没有<title>之类的标签,就不会出现这种情况了。</p><p>以上说的异步请求仅适用于请求单个文件,如果我们请求的是动态数据,并且数据需要以JSON格式返回,上面的方法将会显得力不从心,这是struts2的原生支持就得出马了。</p><p>使用struts2的ajax,必须在项目中引入struts2-json-plugin-2.2.1.jar,在版本2.1.7+都一句绑定在struts2发行包里面了(之前的版本可以在这下载)。记住,要引入struts2-json-plugin-2.2.1.jar。</p><p>这次我们使用另一个例子,模拟加载评论:</p><p>dto对象,Comment.java</p><pre>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;  }  }</pre><p>新的InfoAction.java </p><pre>packagecn.codeplus.action;  importjava.util.ArrayList;importjava.util.List;  importcn.codeplus.po.Comment;  importcom.opensymphony.xwork2.ActionSupport;  publicclassInfoAction extendsActionSupport {  privatestaticfinallongserialVersionUID =1359090410097337654L;  privateList<Comment>comments =newArrayList<Comment>();//没getter and setter方法的属性不会被串行化到JSON  @SuppressWarnings("unused")  privateString title;//!!!使用transient修饰的属性也会被串行化到JSONprivatetransientString content;publicString loadInfo() {  title="123木头人";  content="你是木头人,哈哈。";  loadComments();returnSUCCESS;  }/*** 加载留言信息*/  privatevoidloadComments() {  Comment com1 =newComment();  com1.setContent("很不错嘛");  com1.setId(1);  com1.setNickname("纳尼");  Comment com2 =newComment();  com2.setContent("哟西哟西");  com2.setId(2);  com2.setNickname("小强");  comments.add(com1);  comments.add(com2);  }publicList<Comment>getComments() {returncomments;  }publicvoidsetComments(List<Comment>comments) {this.comments =comments;  }publicstaticlonggetSerialversionuid() {returnserialVersionUID;  }publicString getContent() {returncontent;  }publicvoidsetContent(String content) {this.content =content;  }  }  index.jsp还是那个index.jsp。(*^__^*) 嘻嘻……  struts.xml变化挺大:  <package name="ajaxDemo"extends="json-default"> <action name="loadInfo"class="cn.codeplus.action.InfoAction"method="loadInfo"> <result name="success"type="json"></result> </action> </package></pre><p>在struts.xml中:</p><p>首先,package extends由struts-default转变为json-default,这是必须的,只用在json-default中才包含下面使用的result type为 json。</p><p>然后就是result类型需显示指明为json,result标签内,无需指明视图指向的界面。</p><p>***就是运行结果啦:</p><p>点击“获取”按钮之后:</p><p><img src="/upload/otherpic72/461490.jpg" alt="Struts2中的Ajax开发方法是什么"></p><p>可见comments对象和content对象都被串行化到JSON数据了,不知道是不是版本的问题,很多资料都说使用transient修饰的属性不会被串行化到JSON的。</p><p>为了使content对象不被串行化到JSON,在不能舍弃其getter setter方法的时候,我们可以这样在content的getter方法上面加上注解:@JSON(serialize=false)</p><pre>...  @JSON(serialize=false)publicString getContent() {returncontent;  }publicvoidsetContent(String content) {this.content =content;  }  ...</pre><p>这时的结果如下:</p><p><img src="/upload/otherpic72/461492.jpg" alt="Struts2中的Ajax开发方法是什么"></p><p>@JSON和json类型的result都还有很多可选项,无非就是串行化谁,不串行化谁,返回数据的MIME类型,读者可以自行参考相关文档。</p><p>“Struts2中的Ajax开发方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!</p> <br> 本文标题:Struts2中的Ajax开发方法是什么 <br> 标题网址:<a href="http://kswsj.cn/article/pejegd.html">http://kswsj.cn/article/pejegd.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/dephcdg.html">linux文件awk命令 linux中awk用法详解</a> </li><li> <a href="/article/dephcoh.html">linux接收命令参数 linux命令行传参数</a> </li><li> <a href="/article/dephdpg.html">linux执行scp命令的简单介绍</a> </li><li> <a href="/article/dephcdp.html">c语言函数语法知识点总结 c语言函数基础</a> </li><li> <a href="/article/dephcdi.html">阿里云服务器安装音频 阿里云音频处理</a> </li> </ul> </div> </div> <div class="line"></div> <!--底部--> <footer id="5"> <div class="foot1 container"> <div class="list"> <div class="item"> <a href="javascript:;"> <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span> <strong>关注我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>索要报价</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>我要咨询</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>找到我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>投诉建议</strong> </a> </div> </div> <div class="tel"> <dl> <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br> <span>也许您需要专业的服务,欢迎来电咨询</span> </dl> <dl> <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br> <span>您的需求,是我们前进的动力</span> </dl> </div> </div> <div class="friend"> <div class="container"> <span class="tit">友情链接:</span> <div class="inner"> <a href="https://www.cdcxhl.com/wangzhandingzhi.html" target="_blank">成都定制网站</a><a href="https://www.cdcxhl.com/" target="_blank">网站建设公司</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">成都手机网站建设公司</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">广告投放</a><a href="https://www.cdcxhl.com/seo.html" target="_blank">成都网站推广</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">品牌网站建设</a><a href="https://www.cdcxhl.com/qiye.html" target="_blank">成都企业网站建设公司</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸网站建设</a><a href="https://www.cdcxhl.com/app.html" target="_blank">成都app软件开发公司</a><a href="https://www.cdcxhl.com/google.html" target="_blank">Google优化</a><a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">西云移动机房</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">成都抖音运营</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文营销</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站制作公司</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸营销网站</a><a href="https://www.cdcxhl.com/" target="_blank">网站建设</a><a href="https://www.cdcxhl.com/shop.html" target="_blank">商城网站开发</a> </div> </div> </div> <div class="foot"> <div class="container"> <div class="footNav"> <h3>网站建设</h3> <a href="http://www.cdkjz.cn/wangzhan/qiye/" target="_blank">成都企业网站建设</a><a href="http://www.ncwzjz.com/" target="_blank">南充网站建设</a><a href="http://chengdu.cdcxhl.cn/dingzhi/" target="_blank">成都定制网站建设</a> </div> <div class="footNav"> <h3>服务器托管</h3> <a href="https://www.cdcxhl.com/idc/gadx.html" target="_blank">贵安电信机房</a><a href="https://www.cdcxhl.com/idc/ziyang.html" target="_blank">资阳天府云计算中心</a><a href="https://www.cdcxhl.com/idc/yaan.html" target="_blank">雅安服务器托管</a> </div> <div class="footNav"> <h3>网站制作</h3> <a href="http://www.cdxtjz.com/" target="_blank">网站制作</a><a href="http://seo.cdkjz.cn/wangzhan/" target="_blank">网站制作公司</a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都营销网站制作</a> </div> <div class="footNav"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a><a href="https://www.cdcxhl.com/link/" target="_blank">卖友情链接</a><a href="https://www.cdcxhl.com/service/wenwangwen.html" target="_blank">网络文化经营许可证</a> </div> <div class="fr ecode"> <div class="fl"> <img src="/Public/Home/img/ewm.jpg"> <p>关注企业微信</p> </div> <div class="fr slogan"> <p class="icon"> <a class="ph" href=""><i class="iconfont"></i></a> <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a> </p> <p> <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、 <a href="">营销型网站建设</a>、<a href="">网站建设</a>、 <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a> </p> </div> </div> </div> <div class="bottom container"> <p class="fl"> 版权所有:成都创新互联科技有限公司 备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a> 服务热线:028-86922220 </p> <p class="fr"> <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> </div> </footer> <!--在线咨询--> <div class="fot"> <ul> <li> <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank"> <img src="/Public/Home/img/fot1.png" alt=""> <p>在线咨询</p> </a> </li> <li> <a href="tel:18980820575" target="_blank"> <img src="/Public/Home/img/fot2.png" alt=""> <p>拨打电话</p> </a> </li> </ul> </div> </body> </html> <script> $(".con img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>