应用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);】
<wbr></wbr>
在dwr.xml中设置:
<create creator="struts" javascript="JDwrstruts">
<wbr><wbr><wbr><param name="<span style="color:#0000ff; line-height:21px; word-wrap:normal; word-break:normal">formBean</span>" value="<span style="color:#ff0000; line-height:21px; word-wrap:normal; word-break:normal">dwrForm</span>" /><br> </create></wbr></wbr></wbr>
注:蓝色为固定属性,红色为struts-config.xml中的formbean的name属性值,来对应你的ActionForm.
<wbr></wbr>
页面代码:
<head>
<wbr><wbr><title>JSP for DwrForm form</title><br><wbr><wbr><script src="dwr/interface/JDwrstruts.js"></script><br><wbr><wbr><script src="dwr/engine.js"></script><br><wbr><wbr><script src="dwr/util.js"></script><br><wbr><wbr><script type="text/javascript"><br><wbr><wbr><wbr>function testdwrstruts(){<br><wbr><wbr><wbr><wbr>var uname =<span></span><span style="color:#0000ff; line-height:21px; word-wrap:normal; word-break:normal">DWR.Util.getValue('username')</span>;<br><wbr><wbr><wbr><wbr>alert(uname);<br><wbr><wbr><wbr><wbr>if(uname==' '){<br><wbr><wbr><wbr><wbr><wbr>// 发送请求到服务器,判断用户名是否存在<br><wbr><wbr><wbr><wbr><wbr><span style="color:#ff0000; line-height:21px; word-wrap:normal; word-break:normal">JDwrstruts.getUsername(judgeResult)</span>;<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>function judgeResult(data){<br><wbr><wbr><wbr><wbr><span style="color:#ff0000; line-height:21px; word-wrap:normal; word-break:normal">DWR.Util.setValue('username', data)</span>;<br><wbr><wbr><wbr>}<br><wbr><wbr></script><br><wbr><wbr><br><wbr></head><br><wbr><body><br><wbr><wbr><html:form action="/dwrStruts.do"><br><wbr><wbr><wbr>username:<html:text property="username" οnblur="testdwrstruts();"></html:text><br><wbr><wbr><wbr><html:submit/><html:cancel/><br><wbr><wbr></html:form><br><wbr></body></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
注:取控件值时请使用蓝色处获取,否则容易造成控件失效!
<wbr></wbr>
这里使用的是DWR调用Struts中的formbean,如果想调用Struts中的Action,建议直接调用,不需要使用DWR,Action中构建是业务逻辑,如果非要调用,建议提取业务方法,在Action中自定义方法调用。