struts2内置对ajax支持。。所以可以很方便的应用中使用ajax,struts2是基于了dwr 和dojo的。。我现在用的struts2.0.11只支持到dwr 1.1.2。还不支持2.05版本。。。下面在上篇blog基础上使用ajax进行验证.
1.配置dwr的核心servlet,修改web.xml
<?xml version="1.0" encoding="GBK"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    
<!-- 定义Struts2的FilterDispathcer的Filter -->
    
<filter>
        
<filter-name>struts2</filter-name>
        
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>

    
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
    
<filter-mapping>
        
<filter-name>struts2</filter-name>
        
<url-pattern>/*</url-pattern>
    
</filter-mapping>
    
<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>

</web-app>
2.在需要验证的页面设置ajax主题,register.jsp页:注意有两个地方要设置,特别注意<s:form>要设置
<%@ page contentType="text/html; charset=GBK" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>register</title>
<s:head theme="ajax"/>
</head>
<body>
<s:form action="regist" validate="true" theme="ajax">
<s:textfield name="username" label="用户名"/>
<s:textfield name="pass" label="密码"/>
<s:textfield name="age" label="年龄"/>
<s:textfield name="birthday" label="生日"/>
<s:submit/>
</s:form>
</body>
</html>
 在web-inf下添加一个dwr.xml文件.所有有应用中这个都是固定的
<!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>

其它的文件与上篇文章一样。。这个就可以实现基于ajax的服务端验证