Struts2 Validate Check

Source: Internet
Author: User

In general, when the user registers, we need to verify some of the parameters submitted by the user.

Generally there are two barriers, one in the front page of the use of JS to verify, directly eliminate the abnormal information submitted. The second is to verify the submitted information, not through the return to the registration page and display the error message, we are here to use the Validate method in action to achieve data validation.

The action is inherited from the Actionsupport class, Actionsupport implements the validate interface and has an empty validate method.

You just have to rewrite the Validate method in action. When you run the program, the Validate method is executed first and then the Execute () method is executed.

In the Validate method, there are three ways to increase the actionerror, generally we use the first and third, once the action has an error, it means that the checksum is not passed, the action automatically returns input, You can accept the error prompt in the result page of the input definition.

REGIST.JSP:

<%@ page language= "Java"Import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib prefix= "s" uri= "/struts-tags"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Username:<input type= "text" name= "username" size= "/><br/>"Password:<input type= "password" name= "password" size= "/><br/>"Repassword:<input type= "Password" name= "Repassword" size= "/><br/>"Sex:<input type= "text" name= "Sex" size= "/><br/>"Age :<input type= "text" name= "Age" size= "/><br/>"Birthday:<input type= "text" name= "Birthday" size= "text"/><br/>Graduation:<input type= "text" name= "Graduation" size= "/><br/> <input type=" Submit "value=" Submit "/> &lt ;/form> </body>View Code

Struts.xml:

        class= "Com.shensiyuan.struts.action.RegistAction" >           <result name= "Success" >/ registresult.jsp</result>           <result name= "input" >/regist.jsp</result>        </action>
View Code

Registaction.java:

 Packagecom.shensiyuan.struts.action;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;ImportCom.opensymphony.xwork2.ActionSupport; Public classRegistactionextendsActionsupport {PrivateString username; PrivateString password; PrivateString Repassword; PrivateString sex; Private intAge ; PrivateDate birthday; PrivateDate Graduation;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicString Getrepassword () {returnRepassword; }     Public voidSetrepassword (String repassword) { This. Repassword =Repassword; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; }     PublicDate getgraduation () {returnGraduation; }     Public voidsetgraduation (Date graduation) { This. Graduation =Graduation; }         PublicString Execute () {returnSUCCESS; } @Override Public voidValidate () {/*** string using regular validation, 3 steps: * 1, change string to regular object Pattern p1 = Pattern.compile (REG1);         * 2, Matcher m = p1.matcher (username);         * 3, Boolean b = m.matches ();         * 4 This is equivalent to the preceding 3 steps: Boolean b = pattern.matches ("A*b", "Aaaaab"); */String REG1= "^[0-9a-za-z]{4,8}$"; String REG2= "^ (wo)? man$"; //String reg3= "^ ([01]\\d\\d\\d\\|[ 2][0]\\d\\d)-([0][1-9]|[ 1][0-2]) \\-([0-2][0-9]|[ 3][01]) $ "; Use the following method to use a regular, no need to add a regular string string before and after the//        //System.out.println (username);        Booleanusn=pattern.matches (REG1, username); Booleanps=pattern.matches (REG1, password); Booleanreps=password.equals (Repassword); Booleansx=pattern.matches (REG2, sex); BooleanAg= (age>0&&age<=100); Booleantm=false; if(birthday!=NULL&&graduation!=NULL) {Calendar bir=calendar.getinstance ();             Bir.settime (birthday); Calendar GRA=calendar.getinstance ();             Gra.settime (graduation); TM=(Bir.before (GRA)); }       if(!USN) {            This. Addactionerror ("username should size in four to eight!"); }       if(!PS) {            This. Addactionerror ("Password should size in four to eight!"); }       if(!reps) {            This. Addactionerror ("Repassword should be same as password!"); }       if(!SX) {            This. Addactionerror ("Sex should is man or woman"); }       if(!AG) {            This. Addactionerror ("age should between zero and hundred!"); }       if(!TM) {            This. Addactionerror ("If Exist,birthday shoule before graduation!"); }    }}
View Code

REGISTRESULT.JSP:

<%@ page language= "Java"Import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib prefix= "s" uri= "/struts-tags"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Username:<s:property value= "username"/><br/>Password:<s:property value= "Password"/><br/>Repassword:<s:property value= "Repassword"/><br/>Sex:<s:property value= "Sex"/><br/>Age :<s:property value= "Age"/><br/>Birthday:<s:property value= "Birthday"/><br/>Graduation:<s:property value= "Graduation"/><br/> </body>View Code

A group:

Struts2 Validate Check

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.