1. Attribute-driven
A\ General Set
1 Public classUseractionextendsActionsupport {2 3 PrivateString username;4 PrivateString password;5 6 Public voidSetusername (String username) {7 This. Username =username;8 }9 Ten Public voidSetPassword (String password) { One This. Password =password; A } - - @Override the PublicString Execute ()throwsException { -System.out.println ("Username:" + username + "----Password:" +password); - returnNONE; - } +}
Set Mode
Page layer:
1 <formAction= "${pagecontext.request.contextpath}/user.action"Method= "POST">2 User name3 <inputtype= "text"name= "username" />4 <BR/>5 Password6 <inputtype= "Password"name= "Password" />7 <BR/>8 <inputtype= "Submit"value= "Submit" />9 </form>
JSP page
B\ognl Method implementation
1 <formAction= "${pagecontext.request.contextpath}/user2.action"Method= "POST">2 User name3 <inputtype= "text"name= "User.username" />4 <BR/>5 Password6 <inputtype= "Password"name= "User.password" />7 <BR/>8 <inputtype= "Submit"value= "Submit" />9 </form>
JSP page
Action side:
1 Public classUser2actionextendsActionsupport {2 3 Privateuser user;4 5 PublicUser GetUser () {6 returnuser;7 }8 9 Public voidsetUser (user user) {Ten This. user =user; One } A - @Override - PublicString Execute ()throwsException { the System.out.println (user.tostring ()); - returnNONE; - } -}
Action Side
The encapsulation of Struts2 parameters is divided into two categories:
* Attribute-driven:
* Model-driven:
Property-Driven: A set method that provides properties, encapsulating parameters: (Property driven)
Page:
User name: <input type= "text" name= "username" ><br/>
Action:
public class ParamAction1 extends actionsupport{
Private String username;
public void Setusername (String username) {
This.username = Username;
}
}
Question one: is the global variable provided in action, is it thread-safe or unsafe?
* Global variables are provided, and if the threads are unsafe, the global variables will have a thread security problem. The test provides a construction method. Discovers that each request creates an instance of the action. Indicates that the action is a multi-instance. No thread security issues.
Question two: Who completed the parameter encapsulation?
* <interceptor name= "params" class= "Com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
This is not particularly good! Encapsulates a single property and needs to be encapsulated into a JavaBean object itself. Passed to the business layer.
Second, through the OGNL expression of the package parameters: (Property-driven)
Page:
User name: <input type= "text" name= "User.username" ><br/>
Action:
public class ParamAction2 extends actionsupport{
private user user;
public void SetUser (user user) {
This.user = user;
}
Public User GetUser () {
return user;
}
}
OGNL expression encapsulation, which provides a set and get method for an object in the background action. The Get method must be provided. Get gets the object created to the framework. (encapsulates the encapsulation into different objects.)
The property driver in the
Struts2 (which is actually the parameter in struts2 action that handles the request "old way Servlet API encapsulates data into JavaBean (or beanutils)"), where attribute-driven is the new way