本篇是基于面向对象操作OGNL中的值栈。
1.使用JavaBean封装属性数据:
2.Action实现ModelDriven:
package cn.cvu.action;
import cn.cvu.bean.BeanUser;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 实现ModelDriven类。代替ValueStack存储
*/
public class ActionModel extends ActionSupport implements ModelDriven<BeanUser> {
private BeanUser user = new BeanUser();//必须new出来
public String test(){
//封装属性数据。此userName和userPhone在值栈顶部
//set方法是必须的
user.setUserName("Michael Jackson");
user.setUserPhone("2437-54907431");
System.out.println("test............");
return SUCCESS;
}
/**
* 实现此方法
*/
public BeanUser getModel() {
System.out.println("getModel.........");
return user;
}
}
3.请求参数:
<head>
<title>index.jsp</title>
</head>
<body>
<s:form name="sform_name" namespace="/pkgModel" action="ActionModel_test" method="post">
<!-- 和JavaBean的属性名相同 -->
<s:textfield name="userName" label="用户"></s:textfield>
<s:textfield name="userPhone" label="电话"></s:textfield>
<s:submit type="input" value="提交"></s:submit>
</s:form>
</body>
4.方法二:
- end