预期的效果
查看CompoundRoot的情况,会发现root顶部存储的是MessageAction对象, 所以直接获取id, title, 是找不到的,需要通过msg对象获取
package com.chb.struts2Test.action;
import com.chb.struts2Test.model.Message;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class MessageAction extends ActionSupport implements ModelDriven
{
private static final long serialVersionUID = 1L;
//在MessageAction中不在传入所有属性, 使用Message对象封装
private Message msg;
public MessageAction(){
}
public MessageAction(Message msg) {
this.msg = msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public Message getMsg() {
return msg;
}
public String addInput() {
return "success";
}
public String add() {
return "success";
}
@Override
public Message getModel() {
if (msg == null) {
msg = new Message();
}
return msg;
}
}
我们在来看CompoundRoot的情况,我们回发现Message对象在CompoundRoot的顶部, 那么我们就可以直接获取id, 这就是ModelDriven的功能,将Action中的对象添加到CompoundRoot