Unable to Submit form In struts 2 when using validations

428 views Asked by At

I have been facing a problem for past few hours, that whenever I use validations in my application I get empty values on server-side.

My action file seems like this:

@ParentPackage("interceptions")
@InterceptorRefs({  
    @InterceptorRef("loggingStack")  
})
@Results({@Result(name="login",location="login.layout",type="tiles")})
public class LoginAction extends ActionSupport implements ModelDriven{
    UpUser user = new UpUser();
    String customField;
    @Validations(requiredFields={@RequiredFieldValidator(type=ValidatorType.SIMPLE,fieldName="customField",message="YOU")})   
    @Action(value="/login",results={
            @Result(name="success",location="secure.layout",type="tiles")})
    public String execute() {
        if(user.getUserScreenName()==null)
            return "login";
        System.out.println(userBo.verifyUser(user));
        return "success";
    }
    public Object getModel(){
        return user;
    }
    @VisitorFieldValidator(message="")  
    public UpUser getUser() {
        return user;
    }

    public void setUser(UpUser user) {
        this.user = user;
    }
    public String getCustomField() {
        return customField;
    }
    public void setCustomField(String customField) {
        this.customField = customField;
    }

}

in my struts.xml I have defined my interceptor as:

<package name="interceptions" extends="struts-default" namespace="/">
<result-types>
  <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
   <interceptors>
            <interceptor name="mylogging" 
                class="com.kiosk.lab.interceptor.LoggingInterceptor">
            </interceptor>
            <interceptor-stack name="loggingStack">
                <interceptor-ref name="mylogging" />
                <interceptor-ref name="validation">
    <param name="validateAnnotatedMethodOnly">true</param>
    <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
                  <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>

            </interceptor-stack>
        </interceptors>

</package>

JSP through which I am sending the data is:

<s:submit cssClass="loginButton"/>

Now I am not sure about this problem but, I have even removed ActionSupport from my action and added and tested it in both ways but, no luck. Secondly, if I comment out interception part in my struts.xml and remove validations tag from my action then each and everything works fine. Else customField value at server side is null.

Updates:

After few answers it's in validating the values fine because all the values are going null in java and it's showing actual error message. But, still I have been facing similar issue that it's not submitting values from JSP. As, it always come null and it's keep on posting the same validation error messages.

1

There are 1 answers

3
Alireza Fattahi On BEST ANSWER

You need to add below so that the validation interceptor which is defined in defaultStack can be run

<interceptor-ref name="defaultStack" />