Java from getting started to giving up: a common way to verify STRUTS2

Source: Internet
Author: User

Feel a weekend, people are too lazy not to, today will be a simple content it-----------------------crossing

There are four main ways to verify STRUTS2:

First, write the verification code directly in the function method (do not throw eggs, this method is really a kind of)

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

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

Four, with STRUTS2 verification framework (that is, the use of configuration files to achieve, this method of personal feel should belong to the content of the abandonment, why? Because some of the small partners in front of three methods are used very happy, a learning this way to give up--)

No nonsense, on the code (here on the show to everyone, does not mean I want to live "on" code):

First, write the verification code directly in the function method

This is relatively simple, the steps are also few, I believe that everyone will see.

1.1) Modify the action in need of data validation method, we still take the Add method to operate it, with ripe, handy ...

12345678910111213141516171819202122     // 添加    publicString add() {        //编号必须是六位的数字        if(Integer.toString(singer.getSingerID()).length()!=6){            this.addFieldError("err_id""编号必须是6位的数字");        }        //姓名不能为空        if("".equals(singer.getSingerName().trim())){            this.addFieldError("err_name""姓名不能为空");        }                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()){            returnINPUT;        }        return"add";    }

1.2) Modify the Struts.xml configuration file, plus the case when the return result is input

12345     <!-- 通配符方式调用 -->    <actionname="smng_*" class="com.pxy.action.Hello"method="{1}">        <resultname="{1}">/WEB-INF/jsp/singer_{1}.jsp</result>    <resultname="input">/WEB-INF/jsp/singer_{1}.jsp</result>    </action>

1.3) Modify the JSP page, plus the error display information (display area to decide it yourself)

12     <s:fielderrorfieldName="err_id"></s:fielderror>    <s:fielderrorfieldName="err_name"></s:fielderror>

1.4) Call the corresponding method

In the Address bar type: localhost:8888/strutsdemo/singeradd.action, the final result is as follows:

Today's time is limited, first write so much, after the content, we tell!!!

Java from getting started to giving up: a common way to verify STRUTS2

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.