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://bzwzjz.com/article/pejegd.html">http://bzwzjz.com/article/pejegd.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/dcosjph.html">win7ssh连接阿里云服务器 windows连接阿里云服务器</a> </li><li> <a href="/article/dcosjhd.html">苹果手机怎么用智能聊天 苹果手机gptchat怎么下载</a> </li><li> <a href="/article/dcospoj.html">linux命令大全mv Linux命令大全网站</a> </li><li> <a href="/article/dcosjhi.html">linux命令新增文件夹 linux 新增文件</a> </li><li> <a href="/article/dcosjjs.html">域名ssl怎么认证 域名证书与ssl证书的关系</a> </li> </ul> </div> </div> <div class="footer2"> Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号 <br />友情链接: <a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸网站建设 </a><a href="http://www.36103.cn/" target="_blank">成都网站建设 </a><a href="http://chengdu.cdcxhl.cn/dingzhi/" target="_blank">高端定制网站设计 </a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">移动网站建设 </a><a href="http://chengdu.cdcxhl.cn/jianshe/" target="_blank">品牌网站建设 </a><a href="http://chengdu.cdcxhl.com/dingzhi/" target="_blank">定制网站建设 </a><a href="http://chengdu.cdcxhl.cn/seo/" target="_blank">营销网站建设 </a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">高端网站设计 </a><a href="http://chengdu.kswjz.com/" target="_blank">成都网站设计 </a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站建设 </a><a href="http://chengdu.xwcx.net/mobile/" target="_blank">手机网站建设套餐 </a><a href="http://www.pzhzwz.com/" target="_blank">攀枝花网站设计 </a><a href="http://www.cdkjz.cn/" target="_blank">网站建设 </a><a href="http://www.cqcxhl.com/" target="_blank">重庆网站建设 </a><a href="http://chengdu.cdcxhl.cn/" target="_blank">成都网站设计 </a><a href="https://www.cdcxhl.com/" target="_blank">成都网站设计 </a><a href="https://www.cdcxhl.com/sheji/chengdu.html" target="_blank">四川成都网站设计 </a><a href="http://www.kswcd.com/solution/" target="_blank">网站建设方案 </a><a href="http://m.cdcxhl.cn/qiye/" target="_blank">成都网站建设公司 </a><a href="https://www.cdxwcx.com/wangzhan/mbqiye.html" target="_blank">成都企业网站制作 </a><a href="http://www.kswcd.com/" target="_blank">企业网站设计 </a><a href="http://www.bzwzjz.com/" target="_blank">专业网站设计 </a></div> </body> </html> <script src="/Public/Home/js/wow.min.js"></script> <script> if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))) { new WOW().init(); }; </script> <div class="sidebar"> <ul> <li><a href="http://wpa.qq.com/msgrd?v=3&uin=244261566&site=www.bzwzjz.com&menu=yes" target="_blank"><img src="/Public/Home/images/right_qq.png" /></a></li> <li><a href="http://wpa.qq.com/msgrd?v=3&uin=1683211881&site=www.bzwzjz.com&menu=yes" target="_blank"><img src="/Public/Home/images/qq.png" /></a></li> <li class="tel"><a href="tel:028-86922220"><img src="/Public/Home/images/right_tel.png" /></a></li> <div class="wx"> <span class="weixin"><img src="/Public/Home/images/weixin.jpg"><br> 微信扫一扫在线咨询</span> </div> <li><a class="fx" href="#hero"><img src="/Public/Home/images/right_up.png" /></a></li> </ul> </div> <script type="text/javascript"> $(function () { $('.sidebar .fx').click(function () { $('html,body').animate({ scrollTop: '0px' }, 800); }); }); </script> <script type="text/javascript"> $(document).ready(function () { $("#fancybox-manual-b").click(function () { $.fancybox.open({ href: 'map.html', type: 'iframe', padding: 5 }); }) }); </script> <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>