這篇文章介紹為struts2的表單驗證添加ajax效果。
首先需要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效果了