日期:2014-05-17  浏览次数:20816 次

struts 2.18客户端验证不能编译
目录结构


form的 validate="false"时服务端验证的结果,其中pass111是Login-validation.xml返回的结果。



Login.java
Java code

package org.blackice.news;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {
    static final long serialVersionUID = 5465432167631354L;
    private String username;
    private String password;
    
    public String getUsername(){
        return this.username;
    }

    public void setUsername(String username){
        this.username = username;
    }

    public String getPassword(){
        return this.password;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public String execute(){
        if(this.getUsername()==null||this.getUsername().equals("")){
            this.addFieldError("username", "请输入用户名");
            return INPUT;
        }
        if(this.getPassword()==null||this.getPassword().equals("")){
            this.addFieldError("password", "请输入密码");
            return INPUT;
        }
        return SUCCESS;
    }
    public void validate(){
        if(this.getUsername()==null||this.getUsername().equals("")){
            this.addFieldError("username", "请输入用户名");
        }
        if(this.getPassword()==null||this.getPassword().equals("")){
            this.addFieldError("password", "请输入密码");
        }
    }
}





Login-validation.xml
XML code

<!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="username">
       <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>User</message>
       </field-validator>
    </field>

    <field name="password">
       <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>pass</message>
       </field-validator>
    </field>
</validators>




struts.xml
XML code

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
<package name="org.blackice.news" extends="struts-default">
    <action name="HelloWorld" class="org.blackice.news.HelloWorld">
        <result>/HelloWorld.jsp</result>
    </action>
    <action name="Login" class="org.blackice.news.Login">
        <result name="input">Login.jsp</result>
        <result name="success">index.html</result>
    </action>
</package>
</struts>




validators.xml

XML code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Valida