本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:210
评论数量:205
留言数量:-19
访问次数:923973
建立时间:2007年5月10日




[struts2]Struts 2与AJAX(1)
文章收藏,  网上资源,  软件技术,  电脑与网络

李小白 发表于 2007/10/23 11:57:57

Struts 2与AJAX(第一部分) 在当今——Web 2.0概念铺天盖地的Internet环境下,简易的AJAX集成对于一个成功的WEB框架来说是不可或缺的。因此,Struts 2其中的一个重要的功能(Feature)就是“First-class AJAX support - Add interactivity and flexibility with AJAX tags that look and feel just like standard Struts tags(大意:一流的AJAX支持——通过AJAX标志增加互动性和灵活性,而且使用这些AJAX标志与普通的Struts标志同样简单)”。 实现原理 基于不重新发明轮子的原则,Struts 2并没有开发新的AJAX框架,而是使用时下Java EE平台中比较流行的AJAX框架——Dojo和DWR。 最近在Musachy Barroso等同志的无私奉献下,开发了Struts 2的JSON插件(Plugin),极大地方便了我们输出JSON结果(Result)。 JSON插件(Plugin) 在Struts 2的showcase中的AJAX部分,JSON的结果输出是通过Freemaker模板实现。这种方法在简易性和灵活性上都比不上JSON插件,所以JSON插件值得向大家五星推荐。 下面让我们看一个JSON插件的例子。 首先到以下网址http://code.google.com/p/jsonplugin/downloads/list下载JSON插件的JAR包,并将其加入你的WebContent\WEB-INF\lib下。 接下是本例子的Action代码: 500)this.width=500'>package tutorial;500)this.width=500'>500)this.width=500'>import java.util.ArrayList;500)this.width=500'>import java.util.List;500)this.width=500'>500)this.width=500'>import com.googlecode.jsonplugin.annotations.JSON;500)this.width=500'>import com.opensymphony.xwork2.ActionSupport;500)this.width=500'>500)this.width=500'>500)this.width=500'>public class JsonPluginAction extends ActionSupport 500)this.width=500'>{500)this.width=500'>    private static final long serialVersionUID = -6784977600668791997L;500)this.width=500'>    500)this.width=500'>    private int bookId;500)this.width=500'>    private String title;500)this.width=500'>    private double price;500)this.width=500'>    private List<String> comments;    500)this.width=500'>    private transient String secret1;500)this.width=500'>    private String secret2;500)this.width=500'>500)this.width=500'>    @JSON(name="ISBN")500)this.width=500'>500)this.width=500'>    public int getBookId() 500)this.width=500'>{500)this.width=500'>        return bookId;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public void setBookId(int bookId) 500)this.width=500'>{500)this.width=500'>        this.bookId = bookId;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public List<String> getComments() 500)this.width=500'>{500)this.width=500'>        return comments;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public void setComments(List<String> comments) 500)this.width=500'>{500)this.width=500'>        this.comments = comments;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public double getPrice() 500)this.width=500'>{500)this.width=500'>        return price;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public void setPrice(double price) 500)this.width=500'>{500)this.width=500'>        this.price = price;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public String getTitle() 500)this.width=500'>{500)this.width=500'>        return title;500)this.width=500'>    }500)this.width=500'>    500)this.width=500'>500)this.width=500'>    public void setTitle(String title) 500)this.width=500'>{500)this.width=500'>        this.title = title;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>    @Override500)this.width=500'>500)this.width=500'>    public String execute() 500)this.width=500'>{500)this.width=500'>        bookId = 15645912;500)this.width=500'>        title = "Max On Java";500)this.width=500'>        price = 0.9999d;500)this.width=500'>        comments = new ArrayList<String>(3);500)this.width=500'>        comments.add("It's no bad!");500)this.width=500'>        comments.add("WOW!");500)this.width=500'>        comments.add("No comment!");500)this.width=500'>        secret1 = "You can't see me!";500)this.width=500'>        secret2 = "I am invisible!";500)this.width=500'>        return SUCCESS;500)this.width=500'>    }500)this.width=500'>}清单1 src/tutorial/JsonPluginAction.java 以上代码值得注意的是,通过@JSON的JAVA注释(Annotation),我们可以改变JSON结果的属性名称,另外带有transient修饰符与没有Getter方法的字段(field)都不会被串行化为JSON。 然后,我们来配置一下此Action,代码如下: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>        <package name="Struts2_AJAX_DEMO" extends="json-default">        <action name="JsonPlugin" class="tutorial.JsonPluginAction">            <result type="json" />        </action>                </package></struts>清单2 src/struts.xml 上面配置文件的“package”元素和以往不同的是,它扩展了“json-default”而不是“struts-default”。“json-default”是在jsonplugin-0.11.jar包里的struts-plugin.xml中定义的。该文件同时定义了“json”的结果类型,有兴趣的朋友可以打开此文件看看。 发布运行应用程序,在浏览器中键入:http://localhost:8080/Struts2_Ajax/JsonPlugin.action,出现下载文件对话框,原因是JSON插件将HTTP响应(Response)的MIME类型设为“application/json”。把文件下载下来,用记事本打开,内容如下: {"ISBN":15645912,"comments":["It's no bad!","WOW!","No comment!"],"price":0.9999,"title":"Max On Java"}清单3 例子1输出的JSON串 当然这还不是一个完整的AJAX的例子,下面让我们写一个HTML文件将其完成,HTML代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head>    <title>JSON Plugin</title>    <script type="text/javascript">        var bXmlHttpSupport = (typeof XMLHttpRequest != "undefined" || window.ActiveXObject);         if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {        function XMLHttpRequest() {            var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",                                 "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",                                 "Microsoft.XMLHTTP"];                                         for (var i=0; i < arrSignatures.length; i++) {                try {                            var oRequest = new ActiveXObject(arrSignatures[i]);                                return oRequest;                        } catch (oError) { /*ignore*/ }            }                          throw new Error("MSXML is not installed on your system.");                       }    }                    function retrieveBook() {                if(bXmlHttpSupport) {            var sUrl = 'JsonPlugin.action';            var oRequest = new XMLHttpRequest();            oRequest.onreadystatechange = function() {                if(oRequest.readyState == 4) {                                        var oBook = eval('(' + oRequest.responseText + ')');                    var bookHolder = document.getElementById('bookHolder');                    var sBook = '<p><b>ISBN: </b>' + oBook.ISBN + '</p>';                    sBook += ('<p><b>Title: </b>' + oBook.title + '</p>');                    sBook += ('<p><b>Price: </b>$' + oBook.price + '</p>');                    sBook += ('<b><i>Comments: </i></b><hr/>');                    for(i = 0; i < oBook.comments.length; i++) {                        sBook += ('<p><b>#' + (i + 1) + ' </b>' + oBook.comments[i] + '


阅读全文(1141) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.545 second(s), page refreshed 144785057 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号