日期:2014-05-16  浏览次数:20345 次

prototype+json+struts2 ajax应用
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head>
    <title>test</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
    <script type="text/javascript" src="js/common/prototype.js"></script>
    <script language="javascript" type="text/javascript">
        function getJson()
        {
             var url = 'JSONExample.action';
             var pars = '';
             var myAjax = new Ajax.Request(url,{
                 method:'get',
                 encoding:'UTF-8',
                 parameters:pars,
                 onComplete:function(json){
                     var JSONobj=eval('('+ json.responseText +')');
                     var html="" ;
                     var menuid="";                  
                     html += "<li>" + JSONobj.newName +"</li>";                   
                     var ints = JSONobj.ints;
                     for (var i=0; i< ints.length; i++){
                        html += "<li>" + ints[i] +"</li>";
                     }           
        
                     var map = JSONobj.map;
                     for(array in map){
                        html += "<li>" + array + ":" + map[array] +"</li>";
                     }                   
                     $("item").innerHTML=html;
                 }
             }    
        }
</script>
    <body>
      <input type="button" name="button" id="button" onclick="getJson()"value="点击测试">
      <div id="item"></div>
    </body>
</html>

?

struts.xml 配置文件

?

<package name="example"  extends="json-default">  
  <action name="JSONExample" class="jSONExample" method="executeJson">  
     <result type="json"/>  
   </action>  
</package>
?

这里使用到了 json struts2 结合的插件,请自己去下载那个 jar

?

action

package action;
 
import java.util.HashMap;
import java.util.Map;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport; 
 
public class JSONExample extends ActionSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 4588482034890372410L;
    // 封装请求参数的三个属性
     //format=  例如"yyyy-MM-dd'T'HH:mm:ss"。 
    private String field1;
    private transient String field2;
    
    private String field3;
    // 封装处理结果的属性
    private int[] ints = { 10, 20 };
    @SuppressWarnings("unchecked")
    private Map map = new HashMap();
    private String customName = "custom";
 
    @SuppressWarnings("unchecked")
    public String executeJson() {
        map.put("name1", "sinlff1");
        map.put("name2", "sinlff2");
        map.put("name3", "sinlff3");
        return SUCCESS;
    }
    
    // 三个请求参数对应的setter和getter方法
    @JSON(serialize = false) //format
    public String getField1() {
        return field1;
    }
 
    public void setField1(String field1) {
        this.fiel