Struts2 (4) property-driven and model-driven
1. An overview of the so-called model-driven approach is to use a single JavaBean instance to run through the entire MVC process. In contrast to the property-driven approach, attributes are used as information carriers throughout the MVC process. Attributes cannot be stored independently. They must be attached to an object, which is an Action instance. Simply put, the model driver uses a separate value object to encapsulate Request Parameters and processing results. The property driver uses an Action instance to encapsulate Request Parameters and processing results. Return to the top 2. The LoginAction mentioned in the previous section of the application instance is used as an example. The action in the property driver is:
public class LoginAction extends ActionSupport{ private static final long serialVersionUID = -5377305857336513025L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { if(getUsername().equals("xujian")&&getPassword().equals("123456")) { ActionContext.getContext().getSession().put("user", username); return SUCCESS; } else return ERROR; }}
In model-driven systems, the preceding actions are divided into the following two classes:
/** Used to encapsulate Request Parameters and processing results */public class UserBean {private String username; private String password; private String tip; public String getUsername () {return username ;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getTip () {return tip;} public void setTip (String tip) {this. tip = tip ;}}
An Action class containing the processing logic is also provided:
Public class LoginAction implements Action, ModelDriven <UserBean> {private UserBean model = new UserBean (); @ Override public UserBean getModel () {return model;} @ Override public String execute () throws Exception {if (getModel (). getUsername (). equals ("xujian") & getModel (). getPassword (). equals ("123") {getModel (). setTip ("Haha, login successful"); return SUCCESS;} else return ERROR ;}}
From the code above, we can see that in model-driven mode, Action must implement the ModelDriven interface and implement the getModel () method, this method is used to associate an Action with the corresponding Moel instance. In the struts-default.xml file, we see that the file is configured with two interceptor references params and modelDriven, where params interceptor is responsible for extracting request parameters, if you use the property-driven mode, the modelDriven interceptor is also responsible for passing request parameters to the attributes of the Action instance, while the modelDriven interceptor is responsible for passing request parameters to the attributes of the model. <Interceptor name = "modelDriven" class = "com. opensymphony. xwork2.interceptor. modelDrivenInterceptor "/> <interceptor name =" params "class =" com. opensymphony. xwork2.interceptor. parametersInterceptor "/> Back to Top 3. Summarize the advantages and disadvantages of model-driven and property-driven. The model-driven structure is clear, but the programming is cumbersome. The property-driven programming is concise, but the structure is not clear. Generally, if the form attributes are not one-to-one, you should use the attribute driver. Otherwise, your system must provide two beans, one corresponding to the data submitted by the form, the other is used for the persistent layer.