Let's create a Client-Side validation workflow, step by step.

(info) The validate attribute is set to true.

(info) Some themes do not support client-side validation.

Step 1

Create the form.{snippet:id=clientValidation|lang=xml|javadoc=false|url=struts2/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client.jsp}(info) This case uses the default xhtml theme, so the <s:head > tag is used to link a style sheet.

Step 2

Create the Action class.{snippet:id=quizAction|javadoc=false|lang=java|url=struts2/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/QuizAction.java}

Step 3

Create the validation.xml to configure the validators to be used.{snippet:id=quizValidators|javadoc=false|lang=xml|url=struts2/apps/showcase/src/main/resources/org/apache/struts2/showcase/validation/QuizAction-validation.xml}

Action and Namespace

A correct action and namespace attributes must be provided to the <s:form> tag. For example, if the action named "quizClient" is defined in the "/validation" namespace, the form must be configured as:

XML<s:form method="post" validate="true" action="quizClient" namespace="/validation"> <s:textfield label="Name" name="name"/> <s:textfield label="Age" name="age"/> <s:textfield label="Favorite color" name="answer"/> <s:submit/> </s:form>

While the following will "work" in the sense that the form will function correctly, client-side validation will not. That is because Struts must know the exact namespace and action (rather than a URL) to properly support validation.

XML<s:form method="post" validate="true" action="/validation/quizClient.action"> <s:textfield label="Name" name="name"/> <s:textfield label="Age" name="age"/> <s:textfield label="Favorite color" name="answer"/> <s:submit/> </s:form>
  • No labels

4 Comments

  1. In client side validation if we keep validate="true" and theme="simple" Than clent validation won't works.

    1. That is pointed out at the very start of the page.

  2. This one of the best resource for a new learner like me.

    Where can I find the project of the above example for downloading?

    1. You can always check out the Showcase demo app https://svn.apache.org/repos/asf/struts/struts2/trunk/apps/showcase/ or download struts2-apps-x.x.x.zip package.