<%@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.field1 = field1;
}
@JSON(serialize = false)
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
@JSON(serialize = false) //format
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public void setCustomName(String customName) {
this.customName = customName;
}
// 封装处理结果的属性的setter和getter方法
public int[] getInts() {
return ints;
}
public void setInts(int[] ints) {
this.ints = ints;
}
@SuppressWarnings("unchecked")
public Map getMap() {
return map;
}
@SuppressWarnings("unchecked")
public void setMap(Map map) {
this.map = map;
}
// 使用注释语法来改变该属性序列化后的属性名
@JSON(name = "newName")
public String getCustomName() {
return this.customName;
}
}
分享到:
相关推荐
用prototype+json三级联动
Ajax + JSON + Servlet + prototype.js(实现的一个Ajax实例) 很好的展示了Ajax与JSON的无缝连接与后台的交互。
struts2 prototype ajax验证单个文本框
三合一的js文件,包括json.js,prototype.js,nav.js 三个js文件,很实用
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...
PHP实例开发源码—Prototype+php拖拉裁切图片.zip PHP实例开发源码—Prototype+php拖拉裁切图片.zip PHP实例开发源码—Prototype+php拖拉裁切图片.zip
基于Prototype + Spring + Hibernate实现的自动提示功能,代码只是简单的实例程序,希望对初学者有所帮助,呵呵。
长轮询Prototype+comet源代码
Prototype+CSS手风琴菜单效果,欢迎大家使用
基于PHP的Prototype+php拖拉裁切图片.zip
基于PHP的Prototype+php拖拉裁切图片源码.zip
本系统采用struts2进行控制,hibernate来进行数据操作,prototype进行ajax异步访问。
[其他类别]ProtoType+PHP拖拉裁切图片_cropper.zip
PHP实例开发源码—Prototype+php拖拉裁切图片.zip
NULL 博文链接:https://dodomail.iteye.com/blog/251704
资源列表: 1、CSS2.0中文帮助文档 2、prototype1.6及其1.4版中文帮助文档 3、jQuery1.2及其1.2版中文帮助文档 4、3个javascript帮助文档,但仅有一个为全中文 以上资源均由互联网收集整理,现发出来...
prototype的ajax应用,prototype的ajax应用
prototype整合json实现无刷新验证用户名,有详细注释说明。
包括prototype和prototype的开发笔记!
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...