首先需要DWR servlet(uk.ltd.getahead.dwr.DWRServlet)Dojo和Ajax theme,其中DWR用於正常的校驗Dojo用於處理AJax效果。
接下來,配置dwr,編寫dwr.XML,存放於WEB-INF/下。內容如下
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> <allow> <create creator="new" Javascript="validator"> <param name="class" value="org.apache.struts2.validators.DWRValidator"/> </create> <convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport"/> </allow> <signatures> <![CDATA[ import Java.util.Map; import org.apache.struts2.validators.DWRValidator; DWRValidator.doPost(String, String, Map<String, String>); ]]> </signatures> </dwr>
然後需要把其配置到web程序中,往web.XML中加入如下配置代碼:
<servlet> <servlet-name>dwr</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
最後修改我《struts2表單驗證》文章中的JSp文件edit.JSP
head中加入ajax theme,表單form 加入validate=“true” theme=“ajax”即可,現在就可以運行體驗AJax效果了。