Implement form data validation in Struts 2.0 (Validation)

Source: Internet
Author: User
Tags locale

In the first few articles, some friends suggested that I write an article about the form data validation. As quoted in the beginning of the article, "Writing Secure Code" quotes: "All input is evil", so we should check all the external input. The form is the simplest entry for the application, and the data that it passes in must be validated.

Conversion and validation (conversion & Validation)

In fact, in the last article, I was going to write the contents of the form data checksum, but after repeated thinking, I decided to write the Struts 2.0 converter content first. The reason is that I think the conversion is the basis of the checksum, only after the data has been correctly converted to its corresponding type, we can verify its range of values. For example, I think we can be more clear. Now let's change the first example of the Magician in Converter (Converter)--struts 2.0.

First, starting with the action, the modified code is as follows:

Package Tutorial
Import java.util.Locale;
import com.opensymphony.xwork2.ActionSupport;
Import Com.opensymphony.xwork2.util.LocalizedTextUtil;   
public class HelloWorld extends Actionsupport {
Private String msg;
Private Locale loc = locale.us;   
Public String getmsg () {
return msg;
  
Public Locale Getloc () {
return loc;
  
public void Setloc (Locale loc) {
. loc = loc;
    
@Override
public void Validate () {
System.out.println ("Calling Validate ()");
if (! (Loc.equals (locale.us) | | | loc.equals (LOCALE.CHINA)) {
Addfielderror ("Loc", GetText ("Validation.loc"));
}
}
public void Validateexecute () {
System.out.println ("Calling Validateexecute () by re Flection ");     
}
@Override
Public String execute () {
System.out.println ("Calling Execute ()");
LocalizedteXtutil is the International tool class,<s:text> flag in Struts 2.0, which is
msg = Localizedtextutil.finddefaulttext ("HelloWorld") that implements internationalization by calling it , loc);
return SUCCESS;
}
}

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.