Struts2 data verification and struts2 Verification
I. Why data verification?
For a web application, all application data is collected through a browser, and user input information is very complex. For some users who are not proficient in operations, the input is incorrect, and other network transmission instability, these may cause abnormal input.
Abnormal Input causes abnormal system interruption, and system crash. The application must be able to process the abnormal data received by the presentation layer normally, the application will give a user prompt indicating that the user must enter the required data, that is, filter out the Abnormal Input. We say that the filtering of abnormal data is data validation.
2. How to implement data verification
We can let a custom class inherit from an ActionSupport class. This class is a default Action implementation class, and its fully qualified name is com. opensymphony. xwork2.ActionSupport. this class provides many default methods, including methods for obtaining international information, data verification methods, and methods for processing user requests by default, because the ActionSupport class is the default implementation class of Struts2. if the class attribute is omitted in the Action configuration in xml, the ActionSupport class is accessed. Its execute () method returns SUCCESS directly, and the ActionSupport class also adds support for verification and localization.
Login. jsp
<Div> <! -- Check the output information. Note that the label name must correspond to the method name. --> <s: fielderror fieldName = "errorname"/> <s: fielderror fieldName = "errorpwd"/> </div> <form action = "Login. action "method =" post "> User name: <input type =" text "name =" username "/> password: <input type = "text" name = "userpwd"/> <input type = "submit" value = "Logon"> </form>
LoginAction class
Public class LoginAction extends ActionSupport {private String username; private String userpwd; @ Override public void validate () {if (username. length () = 0 ){
// Add the Information Content addFieldError ("errorname", "username cannot be blank");} if (userpwd. length () = 0) {addFieldError ("errorpwd", "password cannot be blank") ;}@ Override public String execute () throws Exception {if (username. equals ("123") & userpwd. equals ("123") {// obtain a Map <String, Object> map = ActionContext by means of decoupling. getContext (). getSession (); // if the user name and password are entered correctly, save the value to the set, that is, put the value into the session map. put ("name", username); return SUCCESS;} else {return INPUT ;}}
Struts. xml
<Struts> <! -- If the following configuration is added to the configuration file, you do not need to restart tomcat to modify the configuration file later --> <constant name = "struts. devMode "value =" true "/> <package name =" default "namespace ="/"extends =" struts-default "> <action name =" Login "class =" cn. action. loginAction "> <result name =" success "> Login/scuess. jsp </result> <result name = "input"> Login/login. jsp </result> </action> </package> </struts>
How to avoid the tag automatically generated by the Struts2 framework
We can configure this node in struts. xml:
<! -- Set the UI topic. The default value is the XHTML style. -->
<Constant name = "struts. ui. theme" value = "simple"> </constant>
By configuring struts. xml, you can change some constant values to control the behavior of the struts2 framework.