Struts2中Action与外界交互,实现数据传递主要有两种方式:
我们以如下图所示要求
[img]http://05061107cm.iteye.com/upload/picture/pic/35017/ac2c1d6c-1640-339b-a7ae-b4474c954da2.jpg[/img]
一、字段驱动方式(Field-driven)
此种方式是jsp页面里的name,在action中有相应的setName(String name)方法获得值,如下:
二、模型驱动方式(Model-driven)
指通过javabean模型进行数据传递,javabean所封装的属性与表单中的数据一一对应,jvabean成为数据传递的载体
注意:使用模型驱动在jsp页面所采用的格式
1)如果jsp页面是负责取值的,格式为“模型对面名.属性名”
2)如果jsp页面是负责传值的,格式为“模型对面名.属性名”,也可以为“属性名”
或者
我们以如下图所示要求
[img]http://05061107cm.iteye.com/upload/picture/pic/35017/ac2c1d6c-1640-339b-a7ae-b4474c954da2.jpg[/img]
一、字段驱动方式(Field-driven)
此种方式是jsp页面里的name,在action中有相应的setName(String name)方法获得值,如下:
package bean;
public class Userfacade {
private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
public String execute(){
return "success";
}
}
二、模型驱动方式(Model-driven)
指通过javabean模型进行数据传递,javabean所封装的属性与表单中的数据一一对应,jvabean成为数据传递的载体
package bean;
public class Userfacade {
private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
package lee;
import com.opensymphony.xwork2.ActionSupport;
import bean.Userfacade;
public class Login extends ActionSupport {
private Userfacade use;
/**
* @return the use
*/
public Userfacade getUse() {
return use;
}
/**
* @param use the use to set
*/
public void setUse(Userfacade use) {
this.use = use;
}
/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
注意:使用模型驱动在jsp页面所采用的格式
1)如果jsp页面是负责取值的,格式为“模型对面名.属性名”
<s:property value="use.username" />
2)如果jsp页面是负责传值的,格式为“模型对面名.属性名”,也可以为“属性名”
<s:textfield name="use.username" />
<s:textfield name="use.password" />
或者
<s:textfield name="username" />
<s:textfield name="password" />