Basic action of struts2

Source: Internet
Author: User

View code and comments

Loginaction. Java

  1. Package edu. HUST. Action;
  2. Import com. opensymphony. xwork2.actionsupport;
  3. // 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.
  4. // But it generally inherits the actionsupport under the webwork package. Implement some logic processing functions.
  5. Public class loginaction extends actionsupport {
  6. Private Static final long serialversionuid = 1l;
  7. Private string uname;
  8. Private string pword;
  9. /*
  10. * Struts2 takes the value from the page through the Set () method. For example:
  11. * 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.
  12. * When submitting a request, the setusername () method is used to assign values, instead of directly assigning values to attribute variables.
  13. *
  14. * The get () method is used to obtain and print the value of the page from the action.
  15. * <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.
  16. * 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.
  17. *
  18. * This is also the principle of actionform in struts1.3.
  19. */
  20. Public String GetUserName (){
  21. Return uname;
  22. }
  23. Public void setusername (string username ){
  24. This. uname = username;
  25. }
  26. Public String GetPassword (){
  27. Return pword;
  28. }
  29. Public void setpassword (string password ){
  30. This. pword = password;
  31. }
  32. @ Override
  33. Public void validate (){
  34. // If validate () fails, the system will automatically return an input to struts. XML to redirect it to the error display page.
  35. If (this. GetUserName () = NULL | "". Equals (this. GetUserName (). Trim ())){
  36. // 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.
  37. This. addfielderror ("username", "username required ");
  38. }
  39. If (this. GetPassword () = NULL | "". Equals (this. GetPassword (). Trim ())){
  40. This. addfielderror ("password", "Password required ");
  41. }
  42. }
  43. Public String execute (){
  44. // Struts2 customizes some process return flags: The actionsupport class inherits the action interface, which comes from the action interface. See the help documentation.
  45. /*
  46. * Return input --> the action execution require more input in order to succeed;
  47. * Return login --> the action couldn't execute, since the user most was not logged in;
  48. * Return none --> the action execution was successful but do not show a view;
  49. * Return success --> the action execution was successful;
  50. * Return Error --> the action execution was a failure;
  51. */
  52. If ("Forrest". Equals (this. GetUserName (). Trim () & "Vivian". Equals (this. GetPassword (). Trim ())){
  53. Return success;
  54. } Else {
  55. This. addfielderror ("username", "username or password error ");
  56. Return login; // corresponds to <result name = "login">/login. jsp </result> In struts. xml.
  57. }
  58. }
  59. }

Struts. xml

  1. <Struts>
  2. <! -- Indicates that the struts2 package inherits from the Struts-default package -->
  3. <Package name = "struts2" extends = "struts-Default">
  4. <Action name = "login" class = "edu. HUST. Action. loginaction">
  5. <! -- 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. -->
  6. <! -- 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 -->
  7. <! -- 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) -->
  8. <! -- In addition to automatic display of forms, struts2 can also be displayed through the S: fielderror and S: actionerror labels -->
  9. <Result name = "input">/login. jsp </result>
  10. <Result>/result. jsp </result>
  11. <Result name = "login">/login. jsp </result>
  12. </Action>
  13. </Package>
  14. </Struts>

The login page will not be written. Mainly look at comments. Some comments are useful.

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.