"Java from getting started to giving up" Introduction: Common authentication Methods of Struts2 (II.)

Source: Internet
Author: User

The previous time, we finished the " write validation code directly in the functional method" this way, and then we continue to deal with the following three ways.

Second, rewrite the Validate method (Note that this method verifies all the methods in the Class)

The advantage of using the rewrite validation method is that you can write less code!!!!

2.1) Modify the Action class, add the Valiate method to it, and cut the validation code that was previously written in the Add method .

    //Modified Add Method     public string add ()  {         system.out.println ("called the added Method! ");         system.out.println (" No.: "+singer.getsingerid ());         system.out.println ("Name:" +singer.getsingername ());         system.out.println ("Area:" +singer.getarea ());         SYSTEM.OUT.PRINTLN ("message:" +msg);//        if (This.hasErrors ()) {//         return INPUT;//         }        return  "Add";    }     //does not write any return values, but as long as the Addfielderror method is called, the default return is input     @Override     The  public void validate ()  {//number must be a six-digit number  &NBsp;  if (Integer.tostring (Singer.getsingerid ()). Length ()!=6) {     This.addfielderror ("err_id",  "number must be 6 digits");     }    //name cannot be empty     if ("". Equals (Singer.getsingername (). Trim ())) {     This.addfielderror ("Err_name",  "name cannot be empty");     }    }

2.2) Open the corresponding page,localhost:8888/strutsdemo/singeradd.action, the results are as follows:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9D/5F/wKiom1l--djxg4jxAAAexUDbPjU582.png-wh_500x0-wm_ 3-wmp_4-s_1021617958.png "width=" "height=" 473 "alt=" Wkiom1l--djxg4jxaaaexudbpju582.png-wh_50 "/>


Using this method, it is important to note that the validation is now called even if you are accessing another method (other than add).

Let's add a code to the Validate method to see the effect

@Override public void Validate () {//New code SYSTEM.OUT.PRINTLN ("Call authentication Method");//The number must be a six-bit number if (integer.tostring (SI    Nger.getsingerid ()). Length ()!=6) {this.addfielderror ("err_id", "number must be 6 digits");    }//name cannot be empty if ("". Equals (Singer.getsingername (). Trim ())) {This.addfielderror ("Err_name", "Name cannot be empty"); }    }

Next, with Http://localhost:8888/strutsDemo/smng_update.action, call the Update method to see the effects such as:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9D/6C/wKioL1l_-WXDkhUxAAAlkcBdwCE124.png-wh_500x0-wm_ 3-wmp_4-s_4096194191.png "title=" 11.png "alt=" Wkiol1l_-wxdkhuxaaalkcbdwce124.png-wh_50 "/>


We want each validation method to only verify its corresponding operation, how to solve this problem? This is going to be the third way, look

Iii. using the ValidateXxx method (XXX corresponds to the method name of the method to be validated)

On the basis of the previous approach, we changed the Validate method name to Validateadd and removed the override annotation to implement the third method (I like the way, because, no longer post code and. ), you can again access the corresponding function to try.

If there's a problem, then. Solve it yourself, OK, good people do the bottom, if the error, pay attention to the wrong hint, 650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/m02/9d/6c/wkiol1l_- Nyy3eyqaaaiqbsjcwy837.png-wh_500x0-wm_3-wmp_4-s_895077323.png "title=" 12.png "width=" 491 "height=" "border=" 0 " Hspace= "0" vspace= "0" style= "width:491px;height:50px;" alt= "wkiol1l_-nyy3eyqaaaiqbsjcwy837.png-wh_50"/> If you just look at the call or not, please comment the two if statement, you can solve the problem. The end result is that only the Add method invokes the validation:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/9D/6C/wKioL1l_-xuhMGaUAAAHGWn37mo376.png-wh_500x0-wm_ 3-wmp_4-s_2974519391.png "title=" 13.png "width=" "height=" 208 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:250px ; height:208px; "alt=" Wkiol1l_-xuhmgauaaahgwn37mo376.png-wh_50 "/>

As for the fourth way, we do not speak in the introductory article, you crossing if you are interested, you can first study on their own.

Today's content this to the end of the child is not very easy!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" J_0003.gif "/>



This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1952609

"Java from getting started to giving up" Introduction: Common authentication Methods of Struts2 (II.)

Related Article

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.