Using the Jfinal frame validator

Source: Internet
Author: User

Validator is a validation component in the Jfinal framework that provides the check method we use frequently in the validator class, and validator itself implements the interceptor interface, so validator is also equivalent to an interceptor, Use details such as the following:

For example, verify that username and password are empty when logging on, 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) {// Verify that the information entered is empty validaterequiredstring ("name", "Namemsg", "enter username"), validaterequiredstring ("Password", "Pwdmsg", " Please enter the password "); @Overrideprotected void HandleError (Controller c) {c.render ("login.html");}}
Such a simple login validator is finished, then how to use it, very easy, for example, the processing method submitted at logon is login (), using the @before annotations provided by jfinal, you can use the verifier, such as the following:

@Before (loginvalidator.class) public void login () {String Username=getpara ("name"); String Password=getpara ("password"), if ("admin". Equals (username) && "admin". Equals (password)) {RenderText (" Login successful ");} Else{rendertext ("Login Failed");}}

When the login is submitted to the method, first enter the verifier to verify that the submitted field is legitimate, if not legal, then the validator returns the defined page, the legal is entered into the login () method to continue execution, where login () is implemented in the Controller class.

Display error messages in HTML code using an EL expression, enter the second variable on the checksum to:

<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= "Login"/></form>

In addition to verifying that the value is empty, there are other validation methods, such as:

Check the point mailbox is correct, there is no need to pipe verification mailbox of the regular table, in the source code has been implemented Validateemail ("email", "emailmsg", "e-mail format error");//Verify that the URL is correct validateurl ("url "," urlmsg "," URL error ");
Define the regular form to verify the phone number <span style= "White-space:pre" ></span>validateregex ("Phone", "\\b (1[3,5,7,8,9]\\d{9} ) \\b "," Phonekey "," wrong phone number format ");

Here is only a list of two often used, need other to be able to view the source code, in addition to the validator class validation, we can also customize class inheritance and validator class, write their own checksum

Define the Check class yourself, BaseValidator, the class is also abstract class

Package Com.tenghu.core.validator;import Com.jfinal.validate.validator;public abstract class BaseValidator extends validator{//Verify the phone number is in the form private static final String phonepattern= "\\b (0 (\\d{2,3})-\\d{6,9}) \\b";// Check the mobile phone number is the expression of the private static final String mobilepattern= "\\b (1[3,5,7,8,9]\\d{9}) \\b";//Check the phone number of the telephone is the expression of the private static Final String phonemobilepattern= "\\b ((1[3,5,7,8,9]\\d{9}) | ( 0 (\\d{2,3})-\\d{6,9})) \\b ";/** * Verify phone number * @param field Check field * @param errorkey * @param errormsg */protected void Validatep Honepattern (String field,string errorkey,string errormsg) {validateregex (field, Phonepattern, false, Errorkey, ERRORMSG);}  /** * Verify 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 Phone number * @param field * @param errorkey * @param errormsg */protected void Validatephonemobilepattern (String field , String errorkey,string errormsg) {ValidAteregex (field, Phonemobilepattern, false, Errorkey, errormsg);}} 
The main jfinal framework of the validator is finished, the first ramp, said the missing place, welcome to shoot Bricks


Using the Jfinal frame validator

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.