如何使用Struts2验证进行条件验证?

Eri*_*son 5 java validation struts2

Struts2可以用于条件验证吗?如果填写了"其他"复选框,那么字段"otherDetails"不能为空?

请注意,我正在寻找Struts2,而不是Struts1.非常感谢任何一个例子.

Tri*_*ick 2

你也许可以使用 JS 验证。

联合应用程序:

<s:checkbox id="other" name="other" />
<s:textfield id="otherDetails" name="otherDetails"></s:textfield>

<sx:submit 
            id="button_submit"
            name="button_submit"
            onclick="return checkOther();" />   
Run Code Online (Sandbox Code Playgroud)

JS:

function checkOther() {
    if(document.getElementById('other').checked == true) {
        if(document.getElementyById('otherDetails').value.length == 0) {
            //you should trim the value
            alert("Insert other details");
            return false;
        }
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)