首先需要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效果了。