STRUTS2 Study Note Five: Several ways to collect form data

Source: Internet
Author: User

Method One: struts2 support for Modeldriven mode (model-driven mode) STRUTS2 can collect data in a actionform way similar to Struts1, Such a way called Modeldriven mode action needs to implement the Modeldriven interface, implement the Getmodel () method, return the Bean object, so that struts2 will get the object through the Getmodel () method, Use the Set method to populate the object with data to create the user class (Bean object, Pure Java class containing the Geter method and the Seter method) User class
 Packagecom.djoker.struts2; Public classUser {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}

Loginaction class
 Packagecom.djoker.struts2;ImportCom.opensymphony.xwork2.ModelDriven; Public classLoginactionImplementsModeldriven<user>{    PrivateUser User =NewUser ();  PublicString Execute ()throwsException {if("admin". Equals (User.getusername ()) && "Admin". Equals (User.getpassword ())) {            return"Success"; }                return"Error"; } @Override PublicUser Getmodel () {returnuser; }    }

Method Two: In the form field directly to the object assignment (the method must separate the object into a single class, similar to the Modeldriven model-driven mode) does not need to implement the Modeldriven interface, is directly manipulating the object's get and set methods, So you need to provide the object's set and get Methods action class
 Packagecom.djoker.struts2; Public classloginaction{Privateuser User;  PublicUser GetUser () {returnuser; }     Public voidsetUser (user user) { This. user =user; }     PublicString Execute ()throwsException {if("admin". Equals (User.getusername ()) && "Admin". Equals (User.getpassword ())) {            return"Success"; }                return"Error"; }    }

How form forms are written in a JSP page

    <form action= "Login.action" method= "POST" >         User name:<input type= "text" name= "User.username" > <br>         Secret    code:<input type= "password" name= "User.password" ><br>        <input type= " Submit "value=" Login >    </form>

STRUTS2 Study Note Five: Several ways to collect form data

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.