DOJO结合JSON的一段代码

DOJO本身就自带了一些AJAX方面的标签,但是还是想自己结合JSON来实现一些功能,下面是我在学习过程中调试通过了的一些代码。为了记忆,也为了共享。

<%@ 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值