سؤال

I am using Struts 2 Validator framework with XML. But the server side validations are not working. Following is the code snippet.

Struts.xml

<interceptor-stack name="MyStack">
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="debugging"/>
    <interceptor-ref name="profiling"/>
    <interceptor-ref name="scopedModelDriven"/>
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="params">
        <param name="excludeParams">dojo\..*,^struts\..*,.*\\.*,.*\(.*,.*\).*,.*@.*</param>
    </interceptor-ref>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse,reset</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
        <param name="excludeMethods">input,back,cancel,browse,reset</param>
    </interceptor-ref>
</interceptor-stack>

<action name="process" method="process" class="org.web.action.MyAction">
    <interceptor-ref name="MyStack" />
    <result name="success">success.jsp</result>
    <result name="error">error.jsp</result>
    <result name="wait">wait.jsp</result>
    <result name="input">Index.jsp</result>
</action>

MyAction.java

public class MyAction extends ActionSupport {
    private String amount;
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    public String process() throws Exception {
       //some processing done here    
    }
}

MyAction-process-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "@@xwork.validator.dtd@@">
<validators>
    <field name="amount">
        <field-validator type="requiredstring" short-circuit="true">
            <message key="order.amount.required"/>
        </field-validator>
        <field-validator type="amountValidator" short-circuit="true">
            <message key="order.amount.invalid" />
        </field-validator>
    </field>
</validators>

AmountValidator.java

public class AmountValidator extends FieldValidatorSupport {
    public Struts2DonationAmountValidator() {
        super();
    }

    public void validate(Object object) throws ValidationException {
        //some validation check here
    }
}

validators.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "https://s3.amazonaws.com/static/xwork-validator-config-1.0.dtd">
<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
    <validator name="amountValidator" class="org.web.validator.AmountValidator"/>
</validators>

Index.jsp

<input type="text" id="amountTemp" name="amount">

I have put sysouts at the first line of validator as well as in action method. The sysout in AmountValidator.java is not called. Even while debugging, the validation doesn't get called and the control jumps to action method. There is no hint of any exception in console. The action method flow works fine.

Thanks in advance!!!

هل كانت مفيدة؟

المحلول

I found a other solution on http://struts.apache.org/release/2.1.x/docs/validation.html. I changed the name of my MyAction-process-validation.xml to MyAction-processAction-validation.xml and named my action as processAction instead of process. That means instead of method I used action name.

Thanks for your support.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top