posts - 247,  comments - 25,  trackbacks - 0
1.classpath 中加入jsonplugin.jar
2.jsp
<!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] + '</p>');
                    }
                    bookHolder.innerHTML = sBook;
                }
            };
            oRequest.open('POST', sUrl);
            oRequest.send(null);
        }
    }
    </script>
</head>
<body>
    <input type="button" value="Retrieve Book" onclick="retrieveBook()" />
    <div id="bookHolder"></div>
</body>
</html>
3 .Action

package com.test.action;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class JsonPluginAction extends ActionSupport {
    private static final long serialVersionUID = -6784977600668791997L;
   
    private int bookId;
    private String title;
    private double price;
    private List<String> comments;   
    private transient String secret1;
    private String secret2;

    @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;
    }

    public String getTitle() {
        return title;
    }
   
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String execute() throws Exception {
        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;
    }
}

4.struts.xml

<!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="com.test.action.JsonPluginAction">
            <result type="json" />
        </action>           
    </package>
</struts>

posted on 2008-05-08 11:08 林的 阅读(545) 评论(0)  编辑  收藏 所属分类: ajax

只有注册用户登录后才能发表评论。


网站导航:
 
<2008年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

想回家了,呵呵!

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜