Action中值传递方式

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)方法获得值,如下:

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" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值