In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to understand Hibernate Validator". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A better way, Hibernate Validator.
A typical way to validate is to use Commons Validator for simple validation and write some other validation logic in the controller. Commons Validator can generate JavaScript to handle validation in the view. But Commons Validator also has its own drawbacks: it can only deal with simple verification problems, and the verification information is saved to the XML file. Commons Validator is designed to be used with Struts and does not provide a declaration of a simple way to reuse validation between application layers.
When planning a validation strategy, it is not enough to simply deal with errors when they occur. A good design also prevents errors by generating a friendly user interface. Validating in advance can greatly enhance the user's understanding of the application. Unfortunately, Commons Validator does not support this. Suppose you want the HTML file to set the maxlength property of the text field to match the validation, or put a percent sign (%) after the text field to indicate the value of the percentage you want to enter. Typically, this information is hard-coded into HTML documents. If you decide to modify the name attribute to support 75 characters instead of 60 characters, how much do you need to change? In many applications, you usually need to:
◆ updates DDL to increase the length of database columns (through HibernateDoclet, hbm.xml, or Hibernate Annotations).
◆ updates the Commons Validator XML file to increase the * value to 75.
◆ updates all HTML forms related to this field to modify the maxlength property.
A better approach is to use Hibernate Validator. The definition of validation is added to the model layer through comments, as well as support for the included validation processing. If you choose to make full use of all the Hibernate, this Validator can also provide validation at the DAO and DBMS layers. In the sample code shown below, you will perform one more step using reflection and JSP 2.0 tag files to take full advantage of annotations to dynamically generate code for the view layer. This clears the hard-written business logic used in the view.
DateOfBirth is annotated as NotNull and past dates. Hibernate's DDL generation code adds a non-null constraint to this column, as well as a check constraint that requires that the date must be a previous date. The e-mail address is also not empty and must match the format of the e-mail address. This generates a non-empty constraint, but does not generate a check constraint that matches this format.
Public class Contact implements Serializable {public static final int MAX_FIRST_NAME = 30; public static final int MAX_MIDDLE_NAME = 1; public static final int MAX_LAST_NAME = 30; private String fname; private String mi; private String lname; private Date dateOfBirth; private String emailAddress; private Address address; public Contact () {this.address = new Address ();} @ Valid @ Embedded public Address getAddress () {return address } public void setAddress (Address a) {if (a = = null) {address = new Address ();} else {aaddress = a;}} @ NotNull @ Length (min = 1, max = MAX_FIRST_NAME) @ Column (name = "fname") public String getFirstname () {return fname;} public void setFirstname (String fname) {this.fname = fname @ Length (min = 1, max = MAX_MIDDLE_NAME) @ Column (name = "mi") public String getMi () {return mi;} public void setMi (String mi) {this.mi = mi;} @ NotNull @ Length (min = 1, max = MAX_LAST_NAME) @ Column (name = "lname") public String getLastname () {return lname;}
The Hibernate DAO implementation can also use Validation Annotations if necessary. All you need to do is specify validation rules based on Hibernate events in the hibernate.cfg.xml file. If you really want to take shortcuts, you can just catch InvalidStateException exceptions in the service or controller and loop through the InvalidValue array.
This is the end of "how to understand Hibernate Validator". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.