本文的前提是,你愿意將頁面數據封裝為一個對象(是否封裝還得看實際情況,如果頁面數據特別少也沒這個必要)。
封裝頁面數據是否使用ModelDrivenInterceptor有時候還真與個人使用習慣有點關系
看下面的實現action1:
public class VoteAction implements Action, ModelDriven {
。。。。。
/**
* 封裝請求傳入的信息
*/
private Vote vote = new Vote();
。。。。。
/**
*
* @author weip
* @time 19:36:40 2006-5-14
* @return Object
*/
public Object getModel() {
return vote;
}
}
一個使用ModelDrivenInterceptor的action
還有另一種實現action2
public class VoteAction implements Action{
。。。。。
/**
* 封裝請求傳入的信息
*/
private Vote vote = new Vote();
。。。。。
/**
*
* @author weip
* @time 19:36:40 2006-5-14
* @return Object
*/
public Object getVote () {
return vote;
}
}
action1和action2效果完全一樣,只不過實現起來還是有少許差別
1) action1 需要配置ModelDrivenInterceptor,且實現ModelDriven 接口(如果沒有實現此接口,那么配置ModelDrivenInterceptor沒有任何意義),頁面的傳值很簡單<input type="text" name="type" />
這樣就可以將type的值注入到vote的type屬性
2)action2不需要配置ModelDrivenInterceptor,但頁面傳值稍微麻煩一點
<input type="text" name="vote.type" />
如果使用form提交也無所謂,但如果使用url的方式就很累了。到底如何選擇就要看情況了,好像也無關緊要