做过一个小测试,用dojo框架使ajax与struts结合起来。与大家一起分享。
该测试是用ajax的异步传送,提交表单到action中,获取页面文本框中的内容,返回到页面中,实现无刷新提交表单。
代码清单:
test.jsp:
<html>
<head>
<title>Simple jsp page</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dojo/dojo.js"></script>
<script type="text/javascript">
dojo.require("dojo.io.*");
</script>
<script type="text/javascript">
function test(){
//alert("yes");
var formObj = dojo.byId("dojoForm");
var divObj = dojo.byId("resultDiv");
dojo.io.bind({
formNode:formObj,
url:"<%=request.getContextPath()%>/dojoAction.do?method=dealXml",
method:formObj.method,
load:function(type , data ,evt){
//alert(data);
divObj.innerHTML = data;
},
error:function(type,error){
alert(type);
}
})
}
</script>
</head>
<body>
<input type="button" value="ok" οnclick="test();">
<div id="resultDiv" style="background-color:red;width:300;height:200"></div>
<form action="" id="dojoForm" name="dojoForm" method="post">
<input name="text1" id="text1" value="Thanks">
</form>
</body>
</html>
DojoAction:
public class DojoAction extends DispatchAction {
public ActionForward dealXml(ActionMapping mapping , ActionForm form , HttpServletRequest request , HttpServletResponse response){
try {
String text = request.getParameter("text1");
response.getWriter().write(text);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
}
以上为一个jsp页面和一个struts的Action,实现ajax提交表单。
该例子本人已经运行测试过,感兴趣的朋友可以试一下。