You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 22 Next »

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.

Error formatting macro: snippet: java.lang.NullPointerException

(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.

Error formatting macro: snippet: java.lang.NullPointerException

Step 3

Create the validation.xml to configure the validators to be used.

Error formatting macro: snippet: java.lang.NullPointerException

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:

<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.

<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