Three ways to encapsulate page request parameters with the STRUTS2 framework
First type (not recommended)
Is the combination of the action class and the bean, which is relatively simple, but confusing.
Code:
Packagecom.example.action;ImportCom.opensymphony.xwork2.ActionSupport; Public classPersonextendsactionsupport{PrivateString username; PrivateString password; PrivateString Nickname; PublicString GetUserName () {returnusername; } //the method of action PublicString Execute ()throwsException {System.out.println (username+ "," +password+ "," +nickname); returnNONE; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; }}
<action name= "Act1" class= "Com.example.action.Person" > <!-- static injection The equivalent of the Get and set methods that invoke the action action class--
<param name= "nickname" > Visitors </param></action>
The second separation of the action class from the Bean
PackageCom.example.domain;/*** This is the model *@authorSheldonlee **/ Public classStudent {PrivateString username; PrivateString password; PrivateString Nickname; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; } @Override PublicString toString () {return"Student [username=" + Username + ", password=" +Password+ ", nickname=" + nickname + "]"; } }
Packagecom.example.action;Importcom.example.domain.Student;/*** This is the action *@authorSheldonlee **/ Public classstudentaction {PrivateStudent Student; PublicStudent getstudent () {returnstudent; } Public voidsetstudent (Student Student) { This. Student =student; } PublicString Save () {//encapsulate data from a form into a model object//Call service to save data from student to the databaseSystem.out.println (student.tostring ()); return"None"; }}
<body> <form action= "${pagecontext.request.contextpath}/act2" method= "POST" > User name: <input type= "text" name= "Student.username"/><br/> Secret code:<input type= "Password" Name= "Student.password"/><br/> <input type= "text" name= "Student.nickname"/ ><br/> <input type= "Submit" value= "register"/><br/> </form>
The third is the drive model (the action and Bean are also separate) Modeldriven
PackageCom.example.domain;/*** This is the customer model *@authorSheldonlee **/ Public classCustomer {PrivateString username; PrivateString password; PrivateString Nickname; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; } @Override PublicString toString () {return"Customer [username=" + Username + ", password=" +Password+ ", nickname=" + nickname + "]"; } }
Packagecom.example.action;ImportCom.example.domain.Customer;ImportCom.opensymphony.xwork2.ModelDriven; Public classCustomeractionImplementsModeldriven<customer>{ //Action Class Associates a Customer object//in order to Getmodel () this will not return to NULL, so if an object is instantiated PrivateCustomer customer=NewCustomer (); PublicCustomer GetCustomer () {returncustomer; } Public voidSetcustomer (Customer customer) { This. Customer =customer; } PublicString Save () {System.out.println (customer); return"None"; } //before invoking an action method, the framework calls this method first//Object not instantiated here PublicCustomer Getmodel () {//TODO auto-generated Method Stub returncustomer; }}
<form action= "${pagecontext.request.contextpath}/act2" method= "POST" > <!--parameter omitted class name -- User name:<input type= "text" name= "username"/><br/> Secret code:<input type= "password" name = "Password"/><br/> <input type= "text" name= "nickname"/><br/> <input type= "Submit" value= "register"/><br/> </form>
Note that the two-point action class to implement the Modeldriver interface, the action class to be associated with the object should be aware of the instantiation
STRUTS2 Package Request Parameters