search
HomeJavajavaTutorialUsing the Validator framework in Struts


 Every application has the responsibility to ensure that the data they insert into the backend database is legal and valid. After all, if the data these applications rely on is compromised, it will be catastrophic, and the application can still What do you do to keep yourself functioning properly? For example, in an application that uses a formal relational database, each field in the database has its own rules and constraints to ensure that the data stored in it is correct to a certain extent. Any application that uses backend repository data is responsible for protecting the integrity of the data they submit.
 
  Any attempt to insert or update data that does not meet the standards will likely be discovered and rejected. This kind of detection may be spread throughout every corner of the application, and some verification may be performed at the presentation layer. At the business logic layer, business logic objects generally also have business logic verification, and data must also be checked in the background database.
 
 Unfortunately, because this kind of verification is ubiquitous in the application, it causes the application to verify the data to a certain extent in the code redundancy. This is not what the application wants, because this duplication of work in multiple places makes the deployment and maintenance of the application take more time. If these validation rules can be reused throughout the application, it will make the application more flexible. In other words, deployment will be faster, customization will be easier, and the program will be more flexible.
 
 Jakarta Commons Project Validator Framework Introduction
  Validator is an open source project created by David Winterfeldt, and it is also a sub-project of Jakarta Commons. The Commons project mainly provides some reusable components like Validator. Other well-known Commons components include BeanUtils, Digester, Logging framework, etc. Validator version 1.0 was released in early November 2002.
  
 Benefits of using Validator
  .Using the Validator framework has many advantages over generally defining validation rules in the application code, such as:
  .Validation rules can be defined for the application in one place;
  .Validation rules and applications are Loosely coupled;
  . Server-side and client-side validation rules can be defined in the same place;
  . Configuring new validation rules or modifying existing validation rules becomes easier;
  . Supports internationalization;
  . Supports regular expressions;
.Can be used for web applications or standard java applications;
  .Using a declarative method instead of programming;
In addition, the biggest feature of Validator is that it supports pluggability. Later in the article you will see how to use the built-in validation rules of the Validator framework to better complete your work, and more importantly, the Validator framework allows you to customize the validator and insert it into the framework.
 
 The relationship between Struts and Validator
 It should be pointed out that the Validator framework itself is built on the Struts framework. David Winterfeldt, the creator of Validator, discovered in the process of using Struts that the same validation rules need to be used repeatedly in many ActionForm classes, which results in a lot of code redundancy. So he decided to create the Validator framework to eliminate this redundancy, and Validator was born.
 
Although the Validator architecture was originally born for the Struts architecture, it is designed and constructed to be used independently of the Struts architecture. This feature allows you to use this framework in any application, regardless of whether it is Struts-based or not. Just because you don't use the Struts framework will not affect the effect of the Validator architecture on your application. In fact, this is why Validator is part of the Jakarta Commons project and not directly part of the Struts project.
 
Now, let’s integrate this framework into web applications like Struts-based architecture. At the end of the article we will introduce how to apply it to other types of applications, such as EJB-based applications.
  
 Overview of Validator components
  The Validator architecture consists of the following components:
  Validators; A Validator is to execute a Validation rules are a Java class called by the Validator framework. The framework calls this Validaotor class based on the method signature defined in the configuration file. Typically, each Validator class provides a separate validation rule, which can then be combined into more complex rule sets.
 
 Note: Sometimes for convenience, a Validator class can also define multiple validation rules, and each rule is a static method and does not contain any client state information.
 
 The framework provides 14 default validation rules. Sometimes these rules are also called the "basic rules" of the Validator framework. These basic rules are as shown in Table 1:
 Name             
byte, short, integer, Verify whether the value can be converted into the corresponding basic data type
Long, float, double
creditCard Verify whether the input field is a legal credit card number
date Verify whether the input field is a legal date
email Verify the input Check whether the input field can successfully match a regular expression
maxLength Check whether the length of the value is less than or equal to the given maximum length
minLength Check whether the length of the value is greater than or equal to the given minimum length
range Check Whether the range of values ​​is between the maximum value and the minimum value
 required   Verify whether the input field is not empty, or whether the length of the value that does not contain spaces is greater than zero
 Table 1
 As you can see in Table 1, the Validator framework Provides most of the validation rules required by web applications. You can use these existing validation rules to create your own validation profiles. Even so, as we mentioned earlier and will talk about later, you can feel free to add more Validators according to your needs.
 
 Now, let us discuss how to configure and use these basic Validators in an application based on Struts architecture.
What makes the Validator framework flexible is that all validation rules and their specific details are implemented through configuration declarations in external files. Your application does not need to know these specific validation rules. This feature allows you to extend and modify the ruleset without touching your application's source code. This is very important if you want to personalize each installation or when your needs change.
 If you use the Validator framework of Struts 1.1, you will use two configuration files, one is called validator-rules.xml and the other is called validation.xml; in fact, you can also name them arbitrarily, or even put them Merged into one XML file. However, you'd better keep them separate as they each have their own uses.
 
 Note: If you download Validator from the Jakarta website, these two files are not included. These two files can only be found in the Struts download that includes the Validator framework.
 The validator-rules.xml file
 The validator-rules.xml file defines the Validators that the application can use. validator-rules.xml acts as a template, defining the Validators that may be used by all applications.
 
Note: This xml file and another xml file we will discuss below should be placed where the class loader can find them. When we use the Validator framework in a web application, the correct location should be under WEB-INF.
 The validator-rules.xml file is subject to the management of validator-rules_1_1.dtd, which can be downloaded from jakarta.apache.org/struts/dtds/validator-rules_1_1.dtd. We don't want to spend too much time studying the specific details of this file, we only give some basic introduction here.
  The most important elements in the validator-rules.xml file are contained in the element, for example, Example 1:
  Example 1: A simple validator-rules.xml file
 
   ValidatorAction,
      org.apache.commons.validator.Field,
     org.apache.struts.action.ActionErrors,
     javax.servlet.http.HttpServletRequest"
   msg="errors.required"/ >
  
     classname="org.apache.struts.util.StrutsValidator"
   method="validateMinLength"
   methodparams="java.lang.Object,
    org.apache.commons.validator.ValidatorAction,
    org. apache. commons.validator.Field,
      org.apache.struts.action.ActionErrors,
  global>

  
 
 
Each Validator used by the application corresponds to a element. In Example 1, we show you two Validators, one is the request Validator and the other is the minimum length Validator. The element supports many attributes. These properties are necessary to tell the framework which correct class and method this Validator should call. For example, the request Validator element in Example 1 indicates that this Validator will call the validateRequest() method of the org.apache.struts.util.StrutsValidator class. Validator may also depend on another Validator. For example, the minimum length Validator in Example 1 is an example. It contains a depends attribute, which is used to indicate that this Validator will depend on the requesting Validator. The msg attribute specifies a resource binding with a key value that the framework will use to generate the correct error message. The use of resource binding is beneficial for localization of error messages.
  The element also supports the sub-element, allowing you to specify a javascript function to be run by the client. This way server-side and client-side validation can be specified in the same place, which makes maintenance of the application simple.
 
 Validation.xml file
 The second configuration file of the Validator framework is this file called validation.xml. In fact, you can name it whatever you like. The above is the content of using the Validator framework in Struts. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
深度探讨Struts框架的原理与实践深度探讨Struts框架的原理与实践Feb 18, 2024 pm 06:10 PM

Struts框架的原理解析与实践探索Struts框架作为JavaWeb开发中常用的MVC框架,具有良好的设计模式和可扩展性,广泛应用于企业级应用程序开发中。本文将对Struts框架的原理进行解析,并结合实际代码示例进行探索,帮助读者更好地理解和应用该框架。一、Struts框架的原理解析1.MVC架构Struts框架基于MVC(Model-View-Con

解析Struts2框架的工作原理解析Struts2框架的工作原理Jan 05, 2024 am 08:29 AM

探秘Struts2框架的内部机制Struts2是一个流行的JavaWeb应用程序框架,被广泛应用于开发基于MVC架构的Web应用程序。它基于Struts1的基础上进行了大量的改进和优化,提供了更强大、更灵活的功能。在深入探索Struts2框架的内部机制之前,我们需要了解一些基本概念。Struts2框架的核心是MVC(Model-View-Controlle

Java后端开发:使用Java Apache Struts进行API MVC框架开发Java后端开发:使用Java Apache Struts进行API MVC框架开发Jun 17, 2023 am 10:03 AM

随着互联网和智能移动设备的普及,Web应用程序的开发日趋重要。而Java作为一种优秀的跨平台编程语言,受到了开发者们的青睐。在Java后端开发中,使用MVC框架可以帮助我们提高开发效率、降低开发成本。本文将介绍使用JavaApacheStruts进行API的MVC框架开发。一、MVC框架MVC(Model-View-Contro

struts框架的原理和应用有哪些struts框架的原理和应用有哪些Jan 03, 2024 pm 05:31 PM

struts框架的原理:通过采用Java Servlet/JSP技术,实现了基于Java EE Web应用的MVC设计模式的应用框架,它通过采用一个或多个Action类来处理用户请求,并将结果返回给用户。struts框架的应用:1、构建Web应用程序;2、处理表单数据;3、集成第三方库;4、实现国际化;5、构建RESTful API。

Java语言中的Struts框架介绍Java语言中的Struts框架介绍Jun 10, 2023 pm 08:00 PM

Java语言中的Struts框架介绍随着Web应用程序的逐渐普及,比如Web网站和企业应用系统,JavaEE(EnterpriseEdition)架构也逐渐成为了一个主流的选择。在JavaEE架构中,由于MVC(Model-View-Controller)设计模式的流行,Struts框架成为了大家经常使用的Web应用程序框架之一。在本文中,我们将深入探

Struts2框架实现原理的详细解析Struts2框架实现原理的详细解析Feb 21, 2024 pm 05:54 PM

Struts2框架实现原理的详细解析Struts2框架作为一个广泛应用的JavaWeb应用框架,其优秀的设计和性能使得它成为开发者们喜爱使用的工具。了解Struts2框架的实现原理,对于提升开发者对框架的理解和应用水平具有重要意义。本文将通过详细解析Struts2框架的实现原理,并提供具体的代码示例,帮助读者更加深入地了解这一框架。一、Struts2框架简

深入剖析Struts2框架的运行机制深入剖析Struts2框架的运行机制Jan 05, 2024 pm 03:36 PM

深入解析Struts2框架的工作原理Struts2是一个优秀的JavaWeb应用开发框架,其提供了基于MVC(Model-View-Controller)模式的开发方式,帮助开发人员更快速地构建和维护Web应用程序。理解Struts2框架的工作原理对于开发人员来说是非常重要的,本文将通过详细的解析和具体的代码示例,帮助读者深入了解Struts2框架的工作原

探究struts框架的原理及应用探究struts框架的原理及应用Jan 04, 2024 am 08:19 AM

深入解析Struts框架的原理与应用摘要:Struts框架是一个优秀的JavaWeb应用程序开发框架,其基于MVC设计模式,以及一系列的标签库和拦截器,为开发者提供了一种简单、规范的方式来构建Web应用程序。本文将深入解析Struts框架的原理与应用,包括框架的工作原理、主要组件以及通过示例代码来展示其具体的应用。一、Struts框架的工作原理Struts

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.