准备工作:
1、struts2.1.8及以上版本。(因为这个版本后加入了struts2-json-plugin-2.1.8.1)
2、下载jquery-1.7.2.min.js(或其他版本),在JSP中引入<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
要求一:希望在通过ajax后得到json格式数据。
1、JSP代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>
<base href="<%=basePath%>">
<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">
-->
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>
<body>
<a class="topic_author_name" id="topic_author_name">作者</a>
<script type="text/javascript">
$(function(){
$("#topic_author_name").click(function(){
$.ajax({
type:"POST",
async:false,
url:"json/json!author",
data:{"showType":"mydata"},
dataType:"json",
success:function(data) {
alert(data);
var d=eval("("+data+")");
alert(d.h);
alert(d.user.name);
},
});
});
});
</script>
</body>
</html>
2、struts.xml配置如下:
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="json" extends="json-default" namespace="/json">
<action name="json" class="jsonAction">
<result name="author" type="json">
<!-- 这里指定将被Struts2序列化的属性,该属性在action中必须有对应的getter方法 -->
<param name="root">str</param>
</result>
</action>
</package>
</struts>
注意:1、此时为 extends="json-default" 而不是extends="struts-default"
2、type="json"
3、root表示返回的数据是以str为根结构的数据
3、jsonAction如下:
package com.bbs.action;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.bbs.entity.User;
@Component("jsonAction")
@Scope("prototype")
public class JsonAction extends BaseAction {
private String showType; //接收ajax传入的数据
private String str; //返回到ajax的数据
public String author() {
User u = new User();
u.setAge(33);
u.setName("xiao");
JSONObject jsonObj = new JSONObject();
jsonObj.accumulate("user", u);
jsonObj.accumulate("h", "hhh");
str = jsonObj.toString();
return "author";
}
public String getShowType() {
return showType;
}
public void setShowType(String showType) {
this.showType = showType;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
点击弹出三个窗口,其值依次为:
{"user":{"id":0,"sex":0,"email":"","age":33,"name":"xiao","state":"","password":"","registerTime":null},"h":"hhh"}
hhh
xiao
注意:1、 var d=eval("("+data+")"); 表示对json数据转换成map数组格式的数据
要求二:希望在通过ajax后得到对象格式数据。
1、JSP代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>
<base href="<%=basePath%>">
<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">
-->
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>
<body>
<a class="topic_author_name" id="topic_author_name">作者</a>
<script type="text/javascript">
$(function(){
$("#topic_author_name").click(function(){
$.ajax({
type:"POST",
async:false,
url:"json/json!author",
data:{"showType":"mydata"},
dataType:"json",
success:function(data) {
alert(data);
alert(data.user.name);
alert(data.h);
},
});
});
});
</script>
</body>
</html>
2、struts.xml配置如下:
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="json" extends="json-default" namespace="/json">
<action name="json" class="jsonAction">
<result name="author" type="json">
<!-- 这里指定将被Struts2序列化的属性,该属性在action中必须有对应的getter方法 -->
<param name="root">dataMap</param>
</result>
</action>
</package>
</struts>
注意:1、此时为 extends="json-default" 而不是extends="struts-default"
2、type="json"
3、root表示返回的数据是以str为根结构的数据
3、jsonAction如下:
package com.bbs.action;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.bbs.entity.User;
@Component("jsonAction")
@Scope("prototype")
public class JsonAction extends BaseAction {
private String showType; //接收ajax传入的数据
private Map<String, Object> dataMap; //返回到ajax的数据
public String author() {
User u = new User();
u.setAge(33);
u.setName("xiao");
dataMap.put("user",u);
dataMap.put("h",hhh);
return "author";
}
public String getShowType() {
return showType;
}
public void setShowType(String showType) {
this.showType = showType;
}
public Map<String, Object> getDataMap() {
return dataMap;
}
public void setDataMap(Map<String, Object> dataMap) {
this.dataMap = dataMap;
}
}<span style="color:#FF0000;">
</span>
注意:此时ajax返回的data数据为对象而非json字串格式。如直接alert(data)怎会出现 [object Object]的结果!