文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>struts2的ajax支持...

struts2的ajax支持...

时间:2010-08-05  来源:dhdhdh0920

     Struts2相比Struts1,在很多方面有了很大的改善,在AJAX方面的增强是非常明显的。在Struts2的包中有对DOJO和DWR这些AJAX框架的支持。现在又有了一款对JSON输出结果指出的AJAX插件,极大的方便了json结果的输出。

     JSON插件(Plugin)

    Struts 2的showcase中的AJAX部分,JSON的结果输出是通过Freemaker模板实现。这种方法在简易性和灵活性上都比不上JSON插件,所以JSON插件值得向大家五星推荐。

     插件下载地址:http://code.google.com/p/jsonplugin/downloads/list,最新的版本为0.34,下载后将其加入到工程的lib中。注意该jar对struts的版本有要求,所以在工程调试的时候会发现很多包冲突引发的异常。如果包冲突问题无法解决,建议使用struts自带的jar替换,只要将struts2-json-plugin-2.X.X.X.jar和json-lib-2.X.jar替换掉jsonplugin-0.XX.jar即可。因为struts2-json-plugin-2.X.X.X.jar里面的源码和jsonplugin-0.XX.jar源码一样。

     下面看一个简单的例子:

     Action:

     public class JsonPluginAction extends ActionSupport { private static final long serialVersionUID = -9031482373485229634L; private int bookId; private String title; private double price; private List<String> comments; private transient String secret1; private String secret2; private List<String> names; @JSON(name = "ISBN") public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public List<String> getComments() { return comments; } public void setComments(List<String> comments) { this.comments = comments; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @JSON(name = "booktitle") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<String> getNames() { return names; } public void setNames(List<String> names) { this.names = names; } public String execute() { bookId = 15645912; title = "Max On Java"; price = 0.9999d; comments = new ArrayList<String>(3); comments.add("It's no bad!"); comments.add("WOW!"); comments.add("No comment!"); secret1 = "You can't see me!"; secret2 = "I am invisible!"; return SUCCESS; } }

    struts.xml

    <package name="Struts2_AJAX_DEMO" extends="json-default"> <action name="JsonPlugin" class="com.ajax.dh.action.JsonPluginAction" method="execute"> <result type="json" /> </action> </package>

   JavaScript:

<mce:script type="text/javascript"><!-- var xmlHttp; function createXMLHttpRequest() { if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function retrieveBook() { createXMLHttpRequest(); var sUrl = "JsonPlugin.action"; xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { var oBook = eval("(" + xmlHttp.responseText + ")"); var bookHolder = document.getElementById('bookHolder'); var sBook = "<p><b>ISBN: </b>" + oBook.ISBN + "</p>"; sBook += ("<p><b>Title: </b>" + oBook.booktitle + "</p>"); sBook += ("<p><b>Price: </b>$" + oBook.price + "</p>"); sBook += ("<b><i>Comments: </i></b><hr/>"); for ( var i = 0; i < oBook.comments.length; i++) { sBook += ("<p><b>#" + (i + 1) + "</b>" + oBook.comments[i] + "</p>"); } bookHolder.innerHTML = sBook; } }; xmlHttp.open('POST', sUrl, true); xmlHttp.send(null); } function outputbook() { if (xmlHttp.readyState == 4) { var book = eval("(" + xmlHttp.responseText + ")"); var bookHolder = document.getElementById("bookHolder"); var sBook } } // --></mce:script>

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载