DOJO本身就自带了一些AJAX方面的标签,但是还是想自己结合JSON来实现一些功能,下面是我在学习过程中调试通过了的一些代码。为了记忆,也为了共享。
url: "officeJson"表示该请求指向的是一个名叫officeJson得action;
error: 表示该过程出错后调用的处理方法;
load: 表示请求返回结果后调用的处理方法;
mimetype: "text/json"表示返回内容的类型;
formNode: 提交的窗体;
最后通过dojo.io.bind(bindArgs)把该请求发送出去。
要传递的对象需要实现下面这个接口方法。
action中的方法如下:
struts.xml中的配置如下:
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sx:head parseContent="true"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var djConfig = {
isDebug: true
,debugAtAllCosts: true
};
</script>
<script language="Javascript" type="text/javascript">
function onLoad() {
var buttonObj = document.getElementById("myButton");
dojo.event.connect(buttonObj, "onclick",
this, "onclick_myButton");
}
function onclick_myButton() {
var bindArgs = {
url: "officeJson",
error: function(type, data, evt){
alert("An error occurred.");
},
load: function(type, data, evt)
{
alert(data.name);
},
mimetype: "text/json",
formNode: document.getElementById("myForm")
};
dojo.io.bind(bindArgs);
}
</script>
</head>
<body onload="onLoad();">
<form id="myForm">
<input type="text" name="id"/>
<input type="button" id="myButton" value="Submit" />
</form>
</body>
</html>
url: "officeJson"表示该请求指向的是一个名叫officeJson得action;
error: 表示该过程出错后调用的处理方法;
load: 表示请求返回结果后调用的处理方法;
mimetype: "text/json"表示返回内容的类型;
formNode: 提交的窗体;
最后通过dojo.io.bind(bindArgs)把该请求发送出去。
要传递的对象需要实现下面这个接口方法。
public String toJSONString() throws JSONException
{
try
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("id",this.id);
jsonObject.put("name", this.name);
return jsonObject.toString();
} catch (Exception e)
{
e.printStackTrace();
return "";
}
}
action中的方法如下:
public String getJSONString()
{
this.office = service.find(this.id);
inputStream = new ByteArrayInputStream(this.office.toJSONString().getBytes());
return Action.SUCCESS;
}
struts.xml中的配置如下:
<action name="officeJson" class="officeAction" method="getJSONString">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>