Struts in Action Chapter 12, LAMP5W Translation (Lamp5w@yahoo.com.cn)
Validating user input (check user input)
Understand the needs of "data check"
Configure and use Commons Validator
Use multiple pages and localized verification
Write your own Validator
US: Programming Work has become a software engineer strive to build a larger and better fool program and the universe trying to make a competition between more more idiots. At present, the universe is a winner. --Rich Cook
I never know how much it is true. --Bette Midler
12.1
Most web applications need to collect data from the user. Enter the text domain or GUI element such as a menu list, a radio button, multiplex box, and so on. Practice shows that what users entered are not always rational. Some menu options may be mutually exclusive. A phone number may miss a number, the alphabet may be input to the digital field, and the same number may also be input to the field that I want the user to enter letters. This may be because the form of registration data is not smart enough or because the user is not paying attention, but things happen in any case - and the possibility is very large.
Nothing is more frustrated than users from the application's feedback, even because the user first puts the garbage to the app. A rigorous application should prevented all the mistakes that can be predicted in advance, and also protect the user itself. After all, if the user makes a mess, the result is still to help him correct. 12.1.1 Please allow me to reject your input in a traditional app, the data entry control can easily put the poor data on the door, but it is a luxury "modal". ------------------------------------------------definition: A user interface element, when it requires all users to enter an application input "mode". Other elements cannot be accessed before this element is not released. To make the program continue to run, the user either fills the entire modal dialog box or therefore only shuts down the application. Most user interface elements are "non-modal". ---------------------------------------------- WEB application Program, natural is "non-modular", only a small number of options. By default, the HTML element displayed by the browser accepts any input, and a given element does not know anything outside of the form. We can make a joke with JavaScript, but we can't guarantee that users will allow JavaScript in the browser. Of course, we can check it when data arrives at the business logic layer. (More on the application "layer" and business objects, see Chapter 2 of this book.) Many business logic objects have built-in data check capabilities, but most business objects are The data will not be reviewed before receiving data. The business layer method is more trusted guy, and they expect friendly objects to provide reasonable data and work as they claimed, even when business objects are in the most conservative situation, usually everything they can do is Throw an exception. Enter a sick dialogue and users to change his input should not be the responsibility of the business object. Of course, verify data in the context environment is the responsibility of the business object, for example, if a username and password meet the conditions. However, before the data exchanges to the business layer, there is a lot of target verification rules to be implemented. In a distributed application, a business object may reside on a remote machine, creating a connection to and from a client and a server to solve a simple data input error will be very expensive. 12.1.2 Web Layer Check In practice, you often enter the web application framework to provide target verification tasks and reduce the gap between Model and View. In non-mode, in distributed environments, we need to verify the task to do the following:
Make sure the determined field value determines a given value to immediately check the entire Form data in the desired mode and range and return a message list to display localized messages when the value correction behavior between fields To return to the original input. In the case where JavaScript is disabled, the other two important features of the server check verification system is that low coupling and optional client verification low coupling as an actual problem, the input needs to be verified by the controller, but business validity The calibration is tied to the business layer. This means that the verification rules should be stored between the page tags and Java code to allow them to be reviewed and modified without changing any other source code. Keeping calibration rules The low coupling can easily implement synchronization between it and business needs. ------------------------------------------------definition: The degree of coupling involves the association intensity between the two components. Coupling is a supplement to the consolidation. Corporation describes the relationship between the intrinsic content of a component, the purpose is to create components with intrinsic integrity (strong polymerization), smaller, direct, visible, and other components to maintain flexible relationships (low) coupling). [Mcconnell] ----------------------------------------------- - Some verification rules may also require localization. When adding support to a new region, we should be able to update the verification rules, such as the easy update resource binding. When the verification rule may be convenient to the representation layer, it is important to remember that they are actually belonging to the business layer. The verification logic should not be mixed with the code of the layer. The client verifying that the customer is unreliable. Kid a web page submission and bypass the script of any original page is a very easy task. When we cannot rely on client JavaScript check, they are still useful, it can feedback immediately to avoid excessive interaction with the server, saving any time and bandwidth of anyone. So, another ideal feature produces JavaScript and server checks from the same verification rules. When JavaScript is enabled, the input can be verified by the client before committing, if JavaScript is not enabled, then the input will be verified by the server to ensure that no error is not. 12.1.3 Validator Consequences Use Jakarta Commons Validator [ASF, Validator] to find the following inference:
Validator is a framework component that can achieve these requirements - and even more Validator configured by an XML file, which generates the rules defined by the fields in the form. Validator defined by the XML file is also configured as a basic type (for example, Dates, Integers) Prepared Validator is ready-made, if needed, you can create your own verifier regular expression can be used by mode-based check, such as postal coding and telephone numbers to support multi-page and localized verification, you can be any Language Writing Wizard --------------------------------------------- - Definition: Regular expression is based on some modes to match the formula of the string. Regular expressions are applied to many UNIX command lines and programming tools. For more information on regular expressions, please refer to Stephen Ramsay's "Using Regular Expressions" webpage. ------------------------------------------------over you Using Jakarta Commons Validator to bring many benefits: Optimization of resources: When JavaScript is enabled, it can be provided, and the calibration of the server is also guaranteed. Single maintenance: The verification of the client and the server is guaranteed by the same configuration. Scalability: Customer verification can define maintainability through regular expressions or Java code: it is a low coupling relationship between it and the application, does not need to change the page tag or code localization when maintenance: localized verification only Defined with Struts integration when needed: By default, the check will share the message binding of Struts. Localized texts can be concentrated and to easily deploy client verification: To use client checks, you only need to add a separate JSP tag to generate check scripts and submit forms through the script. Easy configuration: Validator is configured using an XML file, just like the deployment description of the web application and the configuration of Struts, but of course, they still have a shortcoming:
Non-mode client verification: The javascript generated by the verifier is non-mode; it does not interfere with the user's input behavior until the form is submitted
Dependency: The check is separated between the fields and the ACTIONFORM attribute. Page tags, actionform, validator, and struts configuration files must be synchronized together
Lack of data conversion and conversion: Validator package does not provide data conversion and conversion function, when needed, data transformation and transformation must be implemented individually.
Keep in mind that using Jakarta Commons Validator in your application is not a spirit of the drug, some calibration can only be implemented by the server. If the server verification fails, the displayed error message is different from the JavaScript message. The intervention of the interface will make the user confused. ------------------------------------------------definition: Data Conversion is to convert data from one type to another, for example from String to Integer. Data Transformation is the intrinsic format that changes the data, for example, to append a punctuation in a string display or to remove unwanted punctuation before it stores. Localization can require conversion data into a display format. ------------------------------------------------ in this chapter In this, we will show how best to use the Commons Validator framework in your application, including the full design of Validator and introduce a simple example. We will focus on every component of Validator, along with frequently needed (original: offen-needed) technology, such as the most important default message, cancel the check, use multi-page workflow, check collection, etc. It is important to emphasize that purpose is important. The data entry check is not a mixed solution (original: Omnibus solution), without accessing MODEL, many types of errors cannot be discovered. We can check if a username and password meet the needs of the business in length and ingredients, but if you want to know if the combination of a username and password is correct, we need to go to the business layer to verify the data service. However, in our request (business layer), the review data is possible correctly, we can save expensive data access transactions, which is good for anyone. ------------------------------------- Note: You may worry, "Then I should use struts. Is the frame or Struts Validator framework to build my app? "At the same time, in fact. Many applications are built using multiple framework components, including some development teams developed at home. Struts is built on the Sun's Java J2SE frame frame framework, the Struts Validator is based on the Struts framework, so you can use many classes like many packages in many packages. . For more information on using the framework system, please refer to Chapter 2 of this book. ------------------------------------ Chapter 4 contains how to set the Validator with Struts1.1 Content, this chapter is how developers will practice Validator practices the wizard in your application. 12.2 Struts Validator overview ---------------------------------- Let us see how struts validator is The components are interacting to provide client and server verification services through the same verification rule settings. You may be surprised that once the validator combines all the items to check how easy your data is.
Table 12.1 lists many sectors that make up the Struts Validator. Table 12.1 Main Struts Validator Components
Component Description Validators (Verifier) Handling native and other common types. Basic Verifiers include "required" (Mask "(matching regular expression), minimum length, maximum length, range, native type, date, email, and credit card number. Custom (or plug-in) Verifier can also be defined. Resource Bundle provides (localized) tags and messages. Share Struts messages by default. XML Configuration File defines a form and verifier for a field that requires a verified field, which can be defined in separate files. JSP TAG (JSP Tag) Generates a JavaScript Verifier for a given form name or Action path. ValidatorFrom automatically checks properties based on the Form bean name (runtime through the validate method specified by the actionmapping parameter). You must be extended to provide a list of attributes of the form ValidatorActionForm automatically check the properties based on the Action path (running by the validate method specified by the actionmapping parameter). It must be expanded to provide the properties expected by the form, Commons Validator is created as an extension of the Struts framework, but since it can be used in the framework, developers contribute it to another Jakarta items - Commons. The Struts release contains a validator package that contains many classes integrated with Commons Validator and Struts. This package is expanded with the Commons Validator package to develop Struts Validator. In this chapter, in this chapter, we mention that Struts Validator is a supercoming of Commons Validator. The Validator package is actually a collection of validator objects written by Java. Each Validator object performs a rule for the properties of another object, in Struts Validator, these objects are ActionForm. The verifier has a standard entry method, like the Struts action, is used to call the checker when needed. The Validator profile allows you to associate one or more checkers for each property in your form. In practice, many applications need to perform a number of checked verifiers. Some fields may require input data, and a postal coding abbreviation may always know the length, and other universal field types include numbers, date, and credit card numbers. Among them, Validator is equipped with many basic validers to handle these general needs. If a basic verifier or a regular expression cannot meet your Validator, you can get into your own validator and insert it into the package. Basic Verifiers only bundled their own plugins, your custom checkor can do any basic verifier can do, or more. Your application needs to be used, basic, or custom, can be specified inside an XML configuration file, usually named Validation.xml. For maintenance work, you can include the rules that associate a Validator to your ActionForm property into a specified file, usually named Validator-Rules.xml. ------------------------------------- 1.0 VS 1.1 in the Validator version of Struts 1.0, use A single validation.xml file contains a Validator definition and a formatter.
Struts1.1 allows you to separate these components to separate files. In this chapter, we will mention the use of Struts1.1 independent files. If you are using struts1.0, all configuration elements remain in a single validation.xml file. ------------------------------------- Check Rules files have a