"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.

123456789101112131415161718192021222324     //修改后的add方法    publicString add() {        System.out.println("调用了添加的方法!");        System.out.println("编号:"+singer.getSingerID());        System.out.println("姓名:"+singer.getSingerName());        System.out.println("地区:"+singer.getArea());        System.out.println("留言:"+msg);//        if(this.hasErrors()){//          return INPUT;//        }        return"add";    }    //没有写任何返回值,但只要调用了addFieldError方法,则默认返回INPUT    @Override    publicvoidvalidate() {        //编号必须是六位的数字        if(Integer.toString(singer.getSingerID()).length()!=6){            this.addFieldError("err_id""编号必须是6位的数字");        }        //姓名不能为空        if("".equals(singer.getSingerName().trim())){            this.addFieldError("err_name""姓名不能为空");        }    }

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

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

12345678910111213     @Override    publicvoidvalidate() {        //新增的一句代码    System.out.println("调用了验证方法");    //编号必须是六位的数字        if(Integer.toString(singer.getSingerID()).length()!=6){            this.addFieldError("err_id""编号必须是6位的数字");        }        //姓名不能为空        if("".equals(singer.getSingerName().trim())){            this.addFieldError("err_name""姓名不能为空");        }    }

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

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. Fix it yourself, okay, good guy. If the error, pay attention to the wrong hint, if only to see the call or not, please put two if statement comments, you can solve the problem. The end result is that only the Add method invokes the validation:

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!!!

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

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.