Verification method of spring MVC annotation

Introduction and description

The annotation verification of spring MVC can directly verify the simple data of view model. Note that it is simple here. If the data verification of model needs some complex business logic, it is difficult to use annotation for verification.

The following is an example of using the annotation verification provided by spring MVC and a customized @ Tel annotation verification. This example has:

1. Support for multilingualism (internationalization)

2. First convert the default data, such as int and date. If you pass in a null value, an exception will be thrown. The default value is given

First look at the configuration:

1、web. xml

There's nothing to say here, just spring Add XML configuration to contextconfiglocation

2、spring. xml

Add conversion service in < MVC: annotation driven / > and then add the system default converter in conversion service, such as intconverter and dateconverter. Of course, other types can be customized, which is global.

Properties supporting multiple languages are added to the validator validator. Of course, spring's multiple languages are based on HTTP header's accept language.

3、Controller

This is a simple controller. In add, there is a @ valid annotation, which is required. Without this, the annotation verification will not work

4、User4. Java model entity class

In addition to @ Tel, other annotations are spring's own. Of course, there are others. Search by yourself

5、message. properties

The corresponding is the message value of the annotation of user4 model. If you need international multilingualism, you only need to add one more message_ en_ US. Properties.

6、@Tel

Create a new interface. Note that the interface java of annotation is written as follows: @ interface

TelValidator:

This is the verification implementation method of @ Tel.

7、Converter

These two are the default converters for global types.

8. Test JSP

Note that the modelattribute attribute value in the form corresponds to the user4 class name, starting with lowercase, otherwise an error will occur

9. Page UI results:

After clicking the Add button:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>