View code and comments
Loginaction. Java
- Package edu. HUST. Action;
- Import com. opensymphony. xwork2.actionsupport;
- // The action of strust2.0 can not inherit any class (this is very different from struts1.3). In this case, the action of struts2.0 is similar to a JavaBean, and only the getter, setter, and execute () methods are supported.
- // But it generally inherits the actionsupport under the webwork package. Implement some logic processing functions.
- Public class loginaction extends actionsupport {
- Private Static final long serialversionuid = 1l;
- Private string uname;
- Private string pword;
- /*
- * Struts2 takes the value from the page through the Set () method. For example:
- * Setusername (string username) --> the method name and parameter name in setusername () must be consistent with the name attribute in the page form. Then pass the value to the uname variable of loginaction through this. uname = username.
- * When submitting a request, the setusername () method is used to assign values, instead of directly assigning values to attribute variables.
- *
- * The get () method is used to obtain and print the value of the page from the action.
- * <Get username> of the method name in GetUserName () must be consistent with the name attribute in the page form. Return the value through return uname. When you use el for a page, you must use username (consistent with GetUserName () instead of uname.
- * When the value is set, the GetUserName () method is used and the reflection technology is used to obtain the value, rather than directly finding the attribute variable for the value.
- *
- * This is also the principle of actionform in struts1.3.
- */
- Public String GetUserName (){
- Return uname;
- }
- Public void setusername (string username ){
- This. uname = username;
- }
- Public String GetPassword (){
- Return pword;
- }
- Public void setpassword (string password ){
- This. pword = password;
- }
- @ Override
- Public void validate (){
- // If validate () fails, the system will automatically return an input to struts. XML to redirect it to the error display page.
- If (this. GetUserName () = NULL | "". Equals (this. GetUserName (). Trim ())){
- // The first parameter of the addfielderror () method must be consistent with the name attribute in the page form. In this way, the error information can be correctly displayed.
- This. addfielderror ("username", "username required ");
- }
- If (this. GetPassword () = NULL | "". Equals (this. GetPassword (). Trim ())){
- This. addfielderror ("password", "Password required ");
- }
- }
- Public String execute (){
- // Struts2 customizes some process return flags: The actionsupport class inherits the action interface, which comes from the action interface. See the help documentation.
- /*
- * Return input --> the action execution require more input in order to succeed;
- * Return login --> the action couldn't execute, since the user most was not logged in;
- * Return none --> the action execution was successful but do not show a view;
- * Return success --> the action execution was successful;
- * Return Error --> the action execution was a failure;
- */
- If ("Forrest". Equals (this. GetUserName (). Trim () & "Vivian". Equals (this. GetPassword (). Trim ())){
- Return success;
- } Else {
- This. addfielderror ("username", "username or password error ");
- Return login; // corresponds to <result name = "login">/login. jsp </result> In struts. xml.
- }
- }
- }
Struts. xml
- <Struts>
- <! -- Indicates that the struts2 package inherits from the Struts-default package -->
- <Package name = "struts2" extends = "struts-Default">
- <Action name = "login" class = "edu. HUST. Action. loginaction">
- <! -- If the verification fails, the validate () method automatically returns the input to jump to the displayed error page. In struts1, the error page is also displayed through input. -->
- <! -- No matter which action, you must configure the input forwarding page. Even if you do not perform the verification, struts2 automatically performs a simple verification. If the verification fails, the error output page will be found based on the input configuration here -->
- <! -- Unlike struts1, error pages can receive error messages without any error tags (S: Form forms integrate with error display) (eg: struts1 contains HTML: errors error tags) -->
- <! -- In addition to automatic display of forms, struts2 can also be displayed through the S: fielderror and S: actionerror labels -->
- <Result name = "input">/login. jsp </result>
- <Result>/result. jsp </result>
- <Result name = "login">/login. jsp </result>
- </Action>
- </Package>
- </Struts>
The login page will not be written. Mainly look at comments. Some comments are useful.