Use validator in the jfinal framework

Source: Internet
Author: User

Validator is a verification component in the jfinal framework. It provides common verification methods in the validator class, And validator implements the interceptor interface. Therefore, validator is equivalent to an interceptor. The specific usage is as follows:

For example, when logging on, check whether the user name and password are empty. The loginvalidator class

Package COM. tenghu. core. validator; import COM. jfinal. core. controller; import COM. jfinal. validate. validator;/*** login validator * @ author Arvin **/public class loginvalidator extends validator {@ overrideprotected void validate (Controller C) {// check whether the entered information is empty validaterequiredstring ("name", "namemsg", "Enter the user name"); validaterequiredstring ("password", "pwdmsg ", "Enter Password") ;}@ overrideprotected void handleerror (Controller c) {C. render ("login.html ");}}
This simple login validator is complete. How can we use it next? For example, the solution submitted during logon is login () and the @ before annotation provided by jfinal is used, you can use the validator as follows:

@ Before (loginvalidator. class) Public void login () {string username = getpara ("name"); string Password = getpara ("password"); If ("admin ". equals (username) & "admin ". equals (password) {rendertext ("Logon successful") ;}else {rendertext ("Logon Failed ");}}

When you log on, submit this method. First, go to the validator to verify that the submitted field is valid. If it is invalid, then the validator returns the defined page and enters login () if it is valid () method. Here, the login () is in the Controller class.

Use the El expression to Display Error information in HTML code. Enter the second variable during verification:

<Form action = "login" method = "Post"> Username: <input type = "text" name = "name"/>$ {namemsg! ''} <Br/> password: <input type =" password "name =" password "/>$ {pwdmsg! ''} <Br/> <input type =" Submit "value =" Logon "/> </form>

In addition to whether the verification value is null, there are other verification methods, such:

// Check whether the idea email address is correct. You do not need to verify the regular expression of the email address here. validateemail ("email", "emailmsg ", "email format error"); // verify that the URL is correct validateurl ("url", "urlmsg", "url error ");
// Customize a regular expression to verify the phone number <span style = "white-space: pre"> </span> validateregex ("phone", "\ B (1 [3, 5, 7, 8, 9] \ D {9}) \ B "," phonekey "," Incorrect Phone Number Format ");

Here we only list two commonly used ones. You can view them in the source code. Apart from the verification in the validator class, you can also define the class inheritance and validator class and write your own validation.

Custom validation class, basevalidator, which is also an abstract class

Package COM. tenghu. core. validator; import COM. jfinal. validate. validator; public abstract class basevalidator extends validator {// Regular Expression of phone number verification Private Static final string phonepattern = "\ B (0 (\ D {2, 3 }) -\ D {6, 9}) \ B "; // verify the regular expression Private Static final string mobilepattern =" \ B (1, 9] \ D {9}) \ B "; // verify the regular expression Private Static final string phonemobilepattern =" \ B (1, 9] \ D {9}) | (0 (\ D {2, 3})-\ D {6, 9}) \ B "; /*** verify the phone number ** @ Param field * @ Param errorkey * @ Param errormsg */protected void validatephonepattern (string field, string errorkey, string errormsg) {validateregex (field, phonepattern, false, errorkey, errormsg );} /*** verify the mobile phone number * @ Param field * @ Param errorkey * @ Param errormsg */protected void validatemobilepattern (string field, string errorkey, string errormsg) {validateregex (field, mobilepattern, false, errorkey, errormsg);}/*** verify the phone number * @ Param field * @ Param errorkey * @ Param errormsg */protected void validatephonemobilepattern (string field, string errorkey, string errormsg) {validateregex (field, phonemobilepattern, false, errorkey, errormsg );}}
The basic jfinal framework validator is complete. When the first ramp is over, we say something is missing. Welcome to make a brick.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.