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