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

struts2结合JSON的Ajax支持
参考资料http://json.org/json-zh.html


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
	<script src="json2.js" type="text/javascript"></script>
	<script type="text/javascript">
		function RegisterAction(){
			//请求地址
			var url='register.action';
			//将表单域中的值序列号
			var params=Form.serialize('registerForm');
			//创建Ajax.request对象,对应发送请求
			var registerAjax=new Ajax.Request(
				url,{
					//请求方式
					method:'post',
					//请求参数
					parameters:params,
					//回调函数
					onComplete:processResponse,
					//是否异步发送
					asynchronous:true
				}
			);
		}
		function processResponse(request){
			//使用JSON对象将服务器响应解析成JSON对象
			var res=JSON.parse(request.responseText);
			//遍历JSON对象的每个属性
			for(var theProperty in res){
				$("show").innerHTML+=theProperty+":"+res[theProperty]+"<br/>"
			}
		}
	</script>
	<body>
		<form id="registerForm" method="post">
			账号:
			<input type="text" name="username" id="username" />
			<br />
			密码:
			<input type="text" name="password" id="password" />
			<br />
			邮箱:
			<input type="text" name="email" id="email" />
			<br />
			地址:
			<input type="text" name="address" id="address" />
			<br />
			<input type="button" value="提交" onclick="RegisterAction()" />
		</form>
		<div id="show"></div>
	</body>
</html>

package demo;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.json.annotations.JSON;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

	/*----------------------------------------------------*/
	private int[] ints = {10, 20};
	
	public int[] getInts() {
		return ints;
	}
	public void setInts(int[] ints) {
		this.ints = ints;
	}
	/*----------------------------------------------------*/
	private Map map=new HashMap();
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	/*----------------------------------------------------*/
	private String username;
	private String password;
	private transient String email;
	private String address;

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	//被注释掉的GET方法不会被序列化
	
//	public String getPassword() {
//		return password;
//	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	/*----------------------------------------------------*/
	
	@JSON(name="newName")
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		map.put("name", "熊佳佳");
		return Action.SUCCESS;
	}
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="demo" extends="json-default">
		<action name="register" class="demo.Register">
			<result type="json"/>
		</action>
		<action name="">
			<result>.</result>
		</action>
	</package>
</struts>