In struts, the validation of page input items based on the configured validation. xml file is well known. This article describes the two validation. xml configuration methods in struts2. You can perform different configurations as needed.
The following describes how to enter firstname, lastname, and age on the login page.
In struts. XML, if successful, go to the success page. If not, go back to the original page.
First, create the userbean file.
File Name: userbean. Java
Package: struts2.login. Bean
File Content:
Package struts2.login. Bean;
Public class userbean {
Private string firstname;
Private string lastname;
Private integer age;
Public String getfirstname (){
Return firstname;
}
Public void setfirstname (string firstname ){
This. firstname = firstname;
}
Public String getlastname (){
Return lastname;
}
Public void setlastname (string lastname ){
This. lastname = lastname;
}
Public void setage (integer age ){
This. Age = age;
}
Public integer getage (){
Return age;
}
}
Create the login action file
File Name: loginaction. Java
Package: struts2.login
File Content:
Package struts2.login;
Import java. util. iterator;
Import java. util. Map;
Import java. util. Set;
Import struts2.login. Bean. userbean;
Import com. opensymphony. xwork2.actionsupport;
@ Suppresswarnings ("serial ")
Public class loginaction extends actionsupport {
Private userbean;
@ Override
Public String execute () throws exception {
// Todo auto-generated method stub
Return success;
}
Public userbean getuserbean (){
Return userbean;
}
Public void setuserbean (userbean ){
This. userbean = userbean;
}
@ Override
Public void validate (){
// Todo auto-generated method stub
Map map = This. getfielderrors ();
Set set = map. keyset ();
For (iterator iter = set. iterator (); ITER. hasnext ();)
{
System. Out. println (Map. Get (ITER. Next ()));
}
}
}
Note the following two points for this file:
1. The action file and bean file are not placed in the same package, which is used to describe the file path of the second xml configuration.
2. The validate method in the action file only serves as a debug error message, which can be removed.
Create a login. jsp file,
File Location: Under the website/work directory
File Content:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Taglib uri = "/Struts-tags" prefix = "S" %>
<HTML>
<S: Form Action = "login" method = "Post">
<S: textfield name = "userbean. firstname" label = "firstname"/>
<S: textfield name = "userbean. lastname" label = "lastname"/>
<S: textfield name = "userbean. Age" label = "Age"/>
<S: Submit/>
</S: Form>
</Html>
Success. jsp file
File Location: Under the website/work directory
File Content:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Taglib uri = "/Struts-tags" prefix = "S" %>
<HTML>
Login success
</Html>
Then there is the Struts. xml file. The content is clear at a glance.
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "default" extends = "struts-Default">
<Action name = "login" class = "struts2.login. loginaction">
<Result name = "success">/work/success. jsp </result>
<Result name = "input">/work/login. jsp </result>
</Action>
</Package>
</Struts>
All preparations are complete.
The following figure shows how to configure the validation. xml file.
1. The first method is to create a file named "action name-validation. xml" under the directory where the action file is located.
File Name: LoginAction-validation.xml
File Location: the same directory as the action file
File Content:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype validators public "-// opensymphony group // xwork validator 1.0.2 //" http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd ">
<Validators>
<Field name = "userbean. firstname">
<Field-validator type = "requiredstring">
<Message> request firstname </message>
</Field-validator>
</Field>
<Field name = "userbean. lastname">
<Field-validator type = "requiredstring">
<Message> request lastname </message>
</Field-validator>
</Field>
<Field name = "userbean. Age">
<Field-validator type = "required">
<Message> request integer </message>
</Field-validator>
<Field-validator type = "int">
<Param name = "min"> 1 </param>
<Param name = "Max"> 150 </param>
<Message> shocould between $ {min} and $ {max} </message>
</Field-validator>
</Field>
</Validators>
The content of the file must be firstname and lastname, and age must be an integer between 1 and 150. For how to write an XML file, refer to the corresponding DTD and help.
Note: 1. When the firstname and other fields are referenced, userbean is added before. This userbean is the variable defined in action, which must be consistent.
2. Multiple verifications for a field can be written together. For more information, see the age field.
3. You can use an expression to reference a set parameter value. Format: $ {parameter name.
After this file is configured, you can run it. After you run it, nothing is input and the result is displayed.
The second method is modified based on the first method.
Modify the content in the validation. xml file.
File Name: LoginAction-validation.xml
File Location: the same directory as the action file
File Content:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype validators public "-// opensymphony group // xwork validator 1.0.2 //" http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd ">
<Validators>
<Field name = "userbean">
<Field-validator type = "visitor">
<Param name = "context"> User </param>
<Param name = "appendprefix"> true </param>
<Message key = "appendprefix"> User's </message>
</Field-validator>
</Field>
</Validators>
This method is to write the project verification of the bean to be verified in another validation file.
The red part:
Userbean specifies the variable name in action.
Visitor is a fixed method.
User is part of the name of another validation file.
Okay. The content of another validation file is as follows:
File Name: UserBean-user-validation.xml
File Location: Consistent with the userbean directory
File Content:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype validators public "-// opensymphony group // xwork validator 1.0.2 //" http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd ">
<Validators>
<Field name = "firstname">
<Field-validator type = "requiredstring">
<Message> request firstname </message>
</Field-validator>
</Field>
<Field name = "lastname">
<Field-validator type = "requiredstring">
<Message> request lastname </message>
</Field-validator>
</Field>
<Field name = "Age">
<Field-validator type = "required">
<Message> request integer </message>
</Field-validator>
<Field-validator type = "int">
<Param name = "min"> 1 </param>
<Param name = "Max"> 150 </param>
<Message> shocould between $ {min} and $ {max} </message>
</Field-validator>
</Field>
</Validators>
Note that the name of file 1 is "Bean class name-context parameter specified value-validation. xml"
2. The field name in the file does not need the userbean prefix.
OK. Run the command.
Summary:
The first method is to validate the action in the validation file. This parameter is applicable when the content of verification is small or the content of verification is repetitive with different actions.
The second type of validation method focuses the validation in the bean validation file. When multiple actions need to verify the content of the same Bean