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;
}
}