DWR+Struts结合的使用

前端之家收集整理的这篇文章主要介绍了DWR+Struts结合的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

应用DWR+Struts要注意以下两点:

1.其中<load-on-startup>的部分要特別注意,ActionServlet要首先初始化,也就是说数字要比DWR的要小.

2.在struts-config.xml中action标签中的scope属性要设置成session,不可以使用默认的request,其原因是:

【在dwr的org.directwebremoting.struts.StrutsCreator中它是这样获取formbean的:
 ActionForm formInstance = (ActionForm) WebContextFactory.get().getSession().getAttribute(formBean);】

在dwr.xml中设置:

<create creator="struts" javascript="JDwrstruts">
<param name="formBean" value="dwrForm" />
</create>

注:蓝色为固定属性,红色为struts-config.xml中的formbean的name属性值,来对应你的ActionForm.

页面代码

<head>
<title>JSP for DwrForm form</title>
<script src="dwr/interface/JDwrstruts.js"></script>
<script src="dwr/engine.js"></script>
<script src="dwr/util.js"></script>
<script type="text/javascript">
function testdwrstruts(){
var uname =DWR.Util.getValue('username');
alert(uname);
if(uname==' '){
// 发送请求到服务器,判断用户名是否存在
JDwrstruts.getUsername(judgeResult);
}
}

function judgeResult(data){
DWR.Util.setValue('username',data);
}
</script>

</head>
<body>
<html:form action="/dwrStruts.do">
username:<html:text property="username" onblur="testdwrstruts();"></html:text>
<html:submit/><html:cancel/>
</html:form>
</body>

注:取控件值时请使用蓝色处获取,否则容易造成控件失效!

这里使用的是DWR调用Struts中的formbean,如果想调用Struts中的Action,建议直接调用,不需要使用DWR,Action中构建是业务逻辑,如果非要调用,建议提取业务方法,在Action中自定义方法调用

猜你在找的Ajax相关文章