网站建设资讯

NEWS

网站建设资讯

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://cdysf.com/article/pejegd.html">http://cdysf.com/article/pejegd.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/gecdjs.html">SpringBoot中怎样获取微信用户信息</a> </li><li> <a href="/article/gecdeo.html">Java中有哪些方法引用</a> </li><li> <a href="/article/gecdje.html">spring-native的介绍及应用</a> </li><li> <a href="/article/gecdge.html">python输入汉字的方法</a> </li><li> <a href="/article/gecdjh.html">保障大型高密度会议wifi网络稳定解决方案是怎样的</a> </li> </ul> </div> </div> </div> <footer> <div class="footop"> <div class="wrap"> <div class="bottomrpw"> <div class="erp arp"> <dl> <dt>ADDRESS</dt> <dd class="address"> <i class="icon"></i> <span class="word">成都市青羊区锦天国际1号楼1002室</span> </dd> </dl> </div> <div class="erp arp"> <dl> <dt>TEL</dt> <dd class="phonum"> <i class="icon"></i> <span class="word en"> <a href="tel:18980820575">18980820575</a> </span> </dd> </dl> </div> <div class="erp crp"> <dl> <dt>OTHER</dt> <dd> <a class="word get-quote">获得报价与方案</a> </dd> <dd> <a href="#" target="_blank" rel='nofollow' class="word" title="付款方式">付款方式</a> </dd> </dl> </div> <div class="erp code-rp"> <dl> <dt>Wechat</dt> <dd class="code-wrap"> <span class="code"> <img src="/Public/Home/images/qr-code.jpg" alt="创帆新辰微信公众号" /> </span> </dd> </dl> </div> </div> </div> </div> <div class="footerbot"> <div class="friendlinks"> <div class="wrap"> <ul class="rpl"> <li><a href="http://m.cdcxhl.com/" title="成都网站制作" target="_blank">成都网站制作</a></li><li><a href="http://www.cdpanxi.com/" title="网站改版设计" target="_blank">网站改版设计</a></li><li><a href="http://www.scyucang.cn/" title="scyucang.cn" target="_blank">scyucang.cn</a></li><li><a href="http://www.36103.cn/baojia/" title="网站设计制作报价" target="_blank">网站设计制作报价</a></li><li><a href="http://www.cxjianzhan.com/" title="成都网站制作" target="_blank">成都网站制作</a></li><li><a href="https://www.cdcxhl.com/sosuo.html" title="网站搜索引擎优化" target="_blank">网站搜索引擎优化</a></li><li><a href="http://chengdu.cdcxhl.com/seo/" title="网站排名" target="_blank">网站排名</a></li><li><a href="http://www.cqcxhl.com/service/ds.html" title="重庆电商网站定制" target="_blank">重庆电商网站定制</a></li><li><a href="https://www.cdxwcx.com/400/" title="400电话办理" target="_blank">400电话办理</a></li><li><a href="http://www.jinhuajc.com/" title="保温橡塑管" target="_blank">保温橡塑管</a></li> </ul> </div> </div> <div class="wrap"> <div class="copyright"> <span class="en">©2007-2025</span> 青羊区创帆新辰信息咨询服务部(个体工商户) <span class="en">ALL RIGHTS RESERVED.</span> <a rel="nofollow" href="http://www.miitbeian.gov.cn" target="_blank">蜀ICP备2025128472号</a> </div> </div> </div> </footer> <div class="fcwrap"> <ul class="rpl clearfix"> <li class="phone"> <a rel="nofollow" target="_blank" href="tel:18980820575"> <i class="icon"></i> <strong>18980820575</strong> </a> </li> <li class="qq"> <a rel="nofollow" target="_blank" href="https://wpa.qq.com/msgrd?v=1&uin=244261566&site=qq&menu=yes"> <i class="icon"></i> <strong>244261566</strong> </a> </li> <li class="back-top"> <a href="javascript:void(0)" rel="nofollow" class="back-to-top"> <i class="icon"></i> <strong>回到顶部</strong> </a> </li> </ul> </div> <!--nav--> <div class="n-Wrap"> <div class="navBar visble show"> <div class="barlogo"> <a href="/" rel="nofollow"> <img src="/Public/Home/images/logo1.png" alt="成都做网站" /> <img src="/Public/Home/images/logo2.png" alt="成都网站设计" /> </a> </div> <div class="bmenu"> <i class="bar-top"><span></span></i> <i class="bar-cen"><span></span></i> <i class="bar-bom"><span></span></i> <i class="bar-left"><span></span></i> <i class="bar-right"><span></span></i> </div> </div> <section class="fixmenu"> <div class="close-bar"> <i class="bar-left"><span></span></i> <i class="bar-right"><span></span></i> </div> <nav class="smph"> <ul> <li class="index-hrefs on"><a href="http://www.cdysf.com/"><font>首页</font></a></li> <li><a href="/about/" rel="nofollow"><font>关于我们</font></a></li> <li><a href="/service/" rel="nofollow"><font>网站建设</font></a></li> <li><a href="/case/" rel="nofollow"><font>网站案例</font></a></li> <li><a href="/solve/" rel="nofollow"><font>网站方案</font></a></li> <li><a href="/news/" rel="nofollow"><font>建站知识</font></a></li> <li><a href="/contact/" rel="nofollow"><font>联系创帆新辰</font></a></li> </ul> <div class="pwrap"> <span class="label">建站热线</font> <strong class="phone"><a href="tel:18980820575">18980820575</a></strong> </div> </nav> </section> </div> <!--end nav--> <script src="/Public/Home/js/hotcss.js"></script> <script type="text/javascript" src="/Public/Home/js/su_new.js"></script> </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>