AJax技术

技术之道,其路之久远,需淡然与仔细(2)

字号+ 作者:H5之家 来源:H5之家 2015-10-20 17:43 我要评论( )

1.EGet.java package lhb;import com.opensymphony.xwork2.ActionSupport;public class EGet extends ActionSupport{private String term;private Terms sampleTerm;private String success;private String resultM

1.EGet.java

package lhb; import com.opensymphony.xwork2.ActionSupport; public class EGet extends ActionSupport { private String term; private Terms sampleTerm; private String success; private String resultMSG; /** * */ private static final long serialVersionUID = 1L; public String execute() throws Exception { initData(); if(term.equals(sampleTerm.getTerm())) { success = "true"; resultMSG = "{\"term\": \""+sampleTerm.getTerm()+"\","+ "\"part\": \""+sampleTerm.getPart()+"\","+ "\"definition\": \""+sampleTerm.getDefinition()+"\","+ "\"quote\": ["+ "\"Is public worship, then, a sin,\","+ "\"That for devotions paid to Bacchus\","+ "\"The lictors dare to run us in,\","+ "\"And resolutely thump and whack us?\""+ "],"+ "\"author\": \""+sampleTerm.getAuthor()+"\"}"; } else{ success = "false"; resultMSG = "fail"; } return SUCCESS; } //初始化数据 private void initData() { String partEAVESDROP = "v.i."; String definitionEAVESDROP = "Secretly to overhear a catalogue of the crimes and vices of another or yourself."; String quoteEAVESDROP[] = {"A lady with one of her ears applied", "To an open keyhole heard, inside,", "Two female gossips in converse free —", "The subject engaging them was she.", "\"I think,\" said one, \"and my husband thinks", "That she's a prying, inquisitive minx!\"", "As soon as no more of it she could hear", "The lady, indignant, removed her ear.", "\"I will not stay,\" she said, with a pout,", "\"To hear my character lied about!\""}; String authorEAVESDROP = "Gopete Sherany"; Terms EAVESDROP = new Terms(); EAVESDROP.setTerm("EAVESDROP"); EAVESDROP.setPart(partEAVESDROP); EAVESDROP.setDefinition(definitionEAVESDROP); EAVESDROP.setQuote(quoteEAVESDROP); EAVESDROP.setAuthor(authorEAVESDROP); sampleTerm = EAVESDROP; } public String getTerm() { return term; } public void setTerm(String term) { this.term = term; } public String getSuccess() { return success; } public void setSuccess(String success) { this.success = success; } public String getResultMSG() { return resultMSG; } public void setResultMSG(String resultMSG) { this.resultMSG = resultMSG; } } 这个action中的数据本人直接配置了,这里只是做一个示范使用。真正的这些数据在项目中一般是存放在数据库中的。由于这主要是jQuery方面的小示例,就不弄那么麻烦了。

2.Terms.java

package lhb; public class Terms { private String term; private String part; private String definition; private String quote[]; private String author; public String getTerm() { return term; } public void setTerm(String term) { this.term = term; } public String getPart() { return part; } public void setPart(String part) { this.part = part; } public String getDefinition() { return definition; } public void setDefinition(String definition) { this.definition = definition; } public String[] getQuote() { return quote; } public void setQuote(String[] quote) { this.quote = quote; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } } 这个类纯粹就是一个pojo类。没有什么特别的方法。

3.struts.xml

这个是struts2的json方式传递配置

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" ""> <struts> <!-- 指定全局国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="i18n"/> <!-- 指定国际化编码所使用的字符集 --> <constant name="struts.i18n.encoding" value="GBK"/> <!-- JSON的action --> <package name="jsonInfo" extends="json-default"> <action name="EGet" class="lhb.EGet"> <result type="json"> <param name="contentType">text/html</param> <param name="includeProperties">success, resultMSG</param> </result> </action> </package> </struts> 这里可以看到includeProperties里所配置的外面一层json,success和resultMSG。这在实际中很好用。如果服务器中没有取得需要的值,虽然ajax访问成功,但是获得的结果并不算成功,因为没有取得需要的值。这里加入了success标示,方便前台jQuery操作。

基于其他方法获取服务器数据从写法上与get基本一致,如post方法、load方法。这里就不再赘述了。

3.动态提交表单

通过jQuery的AJAX支持,可以让我们很方便的动态提交表单而不用刷新页面。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  •  Ajax技术统观(2)

    Ajax技术统观(2)

    2016-01-24 11:42

  • AJAX 技术汇总

    AJAX 技术汇总

    2016-01-24 10:04

  • Ajax技术全解之四(2)

    Ajax技术全解之四(2)

    2016-01-24 10:02

  • 看新鲜,基于Ajax技术网站赏析

    看新鲜,基于Ajax技术网站赏析

    2016-01-23 17:06

网友点评
t