属性驱动:(靠的是ParameterInterceptor拦截器,setValue方法操作值桟属性)
* 在action中设置属性,属性的名称和页面上表单中的name属性的名称保持一致
* 在action中的属性,必须有set和get方法
* 好处: 这样设计可以保证action与servlet容器松耦合
* 如果属性驱动和模型驱动属性名重复,只有模型驱动能取到值,属性驱动为null
属性驱动原理图:
例子:
表单页面
<body>
<s:form action="testParameterInterceptor_Login">
帐号<s:textfield name="userName"/><br>
密码<s:password name="passWord"/><br>
<s:submit value="提交"/>
</s:form>
</body>
Action
public class TestParameterInterceptorAction extends ActionSupport {
private String userName;
private String passWord;
public String execute() throws Exception {
return SUCCESS;
}
public String Login() throws Exception {
System.out.println("userName--->"+userName);
System.out.println("passWord--->"+passWord);
return null;
}
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;
}
}
模型驱动:
* 如果在页面上的表单中,表单的内容很多,这个时候就会在action中写很多属性和属性的set,get方法,这样写代码结构不好
* 采用javabean
* 让该action实现ModelDriver
模型驱动原理图:
例子:
javaBean
public class User {
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;
}
}
表单页面
<body>
<s:form action="testModelDrivenInterceptor_Login">
帐号<s:textfield name="userName"/><br>
密码<s:password name="passWord"/><br>
<s:submit value="提交"/>
</s:form>
</body>
Action
/**
* 要用模型驱动拦截器,就需要实现ModelDriven接口,覆写getModel()
*/
public class TestmodelDrivenInterceptorAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String execute() throws Exception {
return SUCCESS;
}
public String Login() throws Exception {
System.out.println(user.getUserName());
System.out.println(user.getPassWord());
return null;
}
public User getModel() {
return this.user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}