How to validate form using Spring MVC?

Ads

 
 

Share on Google+Share on Google+

Java Coder
How to validate form using Spring MVC?
3 Answer(s)      6 months and 9 days ago
Posted in : Spring

Hi,

I want to validate a form in Spring MVC using the annotation. How to validate a form in Spring MVC. I have form class for username, password, address, and few other fields.

How to validate form using Spring MVC?

How to use annotation for validation?

Thanks

Ads
View Answers

April 11, 2017 at 9:52 PM


Hi,

Spring MVC framework is very flexible framework for developing the web based enterprise applications. We have many options for creating forms and validating.

You can use the JavaScript and validate your form. But if client disables the JavaScript in their browser then there will be issue with your application.

So, server side validation of form is a must. In Spring framework you can use the Hibernate validator framework for validation purpose.

You just have to use the annotation in the Form class and then Spring MVC framework will perform validation for you.

Form example for validating you can use following annotation:

@Size(min = 1, max = 20)
private String userName;

Above annotation validates userName value entered by user. It accepts text with a length between 1 and 20.

If validation error then it will display the error message in the jsp page. In jsp page you can add following code for displaying the error message:

<td>User Name:<font color="red"><form:errors path="userName" /></font></td>

You can view complete video tutorial at Spring 3 MVC Validation Example.

Thanks



April 11, 2017 at 9:52 PM


Hi,

Spring MVC framework is very flexible framework for developing the web based enterprise applications. We have many options for creating forms and validating.

You can use the JavaScript and validate your form. But if client disables the JavaScript in their browser then there will be issue with your application.

So, server side validation of form is a must. In Spring framework you can use the Hibernate validator framework for validation purpose.

You just have to use the annotation in the Form class and then Spring MVC framework will perform validation for you.

Form example for validating you can use following annotation:

@Size(min = 1, max = 20)
private String userName;

Above annotation validates userName value entered by user. It accepts text with a length between 1 and 20.

If validation error then it will display the error message in the jsp page. In jsp page you can add following code for displaying the error message:

<td>User Name:<font color="red"><form:errors path="userName" /></font></td>

You can view complete video tutorial at Spring 3 MVC Validation Example.

Thanks



April 11, 2017 at 9:55 PM


HI, Above tutorial will display error message in the following format:

Check all tutorials at:

Spring Framework Tutorial

The Complete Spring Tutorial

Thanks


Ads









Related Tutorials/Questions & Answers:
Advertisements

Ads

 
Advertisement null

Ads