I am using xml based validation.
Problem:
I don't want validation to perform on input form when page loaded first time because all the fields are blank for user to fill in. Suppose for register form to add new student. It should perform once I click the button.
Note: I want to retain same url even when I do the validation on button click. New form URL is http://localhost:8000/Struts2_Spring_Crud/student/add
and if validation fail even than the url should be same.
struts.xml
<default-action-ref name="list"/>
<action name="list" class="com.myapp.actions.StudentAction" method="getAllStudents">
<!--<interceptor-ref name="myInterceptor"/>-->
<result name="success" type="tiles">/student.list.tiles</result>
</action>
<!--<action name="add">
<result type="tiles">/student.edit.tiles</result>
</action>-->
<action name="add" class="com.myapp.actions.StudentAction" method="insertOrUpdateStudent">
<result name="success" type="redirectAction">list</result>
<result name="input" type="tiles">/student.edit.tiles</result>
</action>
input form
<s:fielderror/>
<s:form action="add" method="POST">
<s:label name="name" value="Name *"/>
<s:textfield name="student.name" value="%{student.name}"/>
<s:fielderror fieldName="student.name"/>
<s:label name="age" value="Age *"/>
<s:textfield name="student.age" value="%{student.age}"/>
<s:submit name="saveForm" value="#title"/>
</s:form>
EDITED: If I am adding excludeMethods than this url http://localhost:8000/Struts2_Spring_Crud/student/add
is sending me to http://localhost:8000/Struts2_Spring_Crud/student/list
and my add form is not showing.
<action name="add" class="com.myapp.actions.StudentAction" method="insertOrUpdateStudent">
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<result name="success" type="redirectAction">list</result>
<result name="input" type="tiles">/student.edit.tiles</result>
</action>
See Question&Answers more detail:os