Struts ajax验证器无法正常工作

时间:2017-05-17 12:36:12

标签: java ajax jsp struts2

我的struts 2 ajax验证器不起作用。所有文件和jar都在正确的位置.IF我把正确的值放在文本框中,验证仍然会触发,我尝试修复代码中的小变化,但问题仍然存在

的index.jsp

<%@ taglib  uri="/struts-tags" prefix="s"%>  
<%@ taglib uri="/struts-dojo-tags" prefix="d"%>  
<html>  
<head>  
<d:head/>  
</head>  
<body>  
<marquee>Registration Form.............</marquee>  

<s:form action="register">  
<s:textfield name="name" label="Username"></s:textfield>  
<s:textfield name="email" label="Email ID"></s:textfield>  
<s:password name="password" label="Password"></s:password>  
<d:submit validate="true" type="image" src="register-now.jpg">  
</d:submit>  
</s:form>  

</body>  
</html>  

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.RegisterAction">
<interceptor-ref name="jsonValidationWorkflowStack"></interceptor-ref>  

<result name="success">welcome.jsp</result>  
<result name="input">index.jsp</result>  
</action>  
</package>

</struts>

RegisterAction.java

package com;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{  
    private String email;  


    public String getEmail() {  
        return email;  
    }  


    public void setEmail(String email) {  
        this.email = email;  
    }  


    public String execute(){  
        return "success";  
    }  

    }  

RegisterAction-Validator.xml

<?xml version="1.0" encoding="UTF-8"?>  

<!DOCTYPE validators PUBLIC   
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  

<validators>  

    <field name="name">  
    <field-validator type="requiredstring">  
    <message>Name can't be blank</message>  
    </field-validator>  
    </field>  

    <field name="email">  
    <field-validator type="requiredstring">  
    <message>Email ID can't be blank</message>  
    </field-validator>  

    <field-validator type="email">  
    <message>Please enter a valid email ID</message>  
    </field-validator>  
    </field>  

    <field name="password">  
    <field-validator type="requiredstring">  
    <message>Password can't be blank</message>  
    </field-validator>  
    <field-validator type="stringlength">  
    <param name="minLength">5</param>  
    <param name="maxLength">10</param>  
    <message>Password can't be less than 5 or greater than 10</message>  
    </field-validator>  

    </field>  

    </validators>      

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2Starter</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

确保在验证发生时 - 堆栈上实际存在值。如果没有那么你的拦截器可以在params拦截器使表单值可访问之前被调用。

https://struts.apache.org/docs/validation-interceptor.html

仔细检查是否正在尝试从您的操作中获取值。如果是,它将需要RegisterAction中所有表单值的getter / setter。姓名/密码不仅仅是电子邮件。