Action中接收參數的三種方法
1. 使用action的屬性接收參數.(在action中定義和參數名相同的屬性,生成getter/setter方法,在相應的方法中可以直接使用屬性,action中的屬性和url中的參數名是一一對應的,不過需要注意一點.參數的名稱,應該是和方法名相同,而不是屬性名!)
2. 使用DoMainModel接收參數(在action中定義實體類的引用,生成getter/setter方法,在傳參數時,加上引用的方法名即可)
package com.test.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.test.entitys.Users;
public class UserAction extends ActionSupport {
//實體類對象
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
private Users user;
public String add() throws Exception{
System.out.println("name="+user.getUserName());
System.out.println("card="+user.getUserCard());
return SUCCESS;
}
public String delete(){
return SUCCESS;
}
}
訪問路徑為:

后臺輸出結果:
name="123"
card=222
3. 使用ModelDriven傳遞參數(在action中實現ModelDriven接口。注意,此時action中的實體對象就需要自己new出來了,在訪問路徑上,就不再需要user.userName了,直接userName就行了)
看它的源碼:
/*
* Copyright (c) 2002-2007 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.xwork2;
/**
* ModelDriven Actions provide a model object to be pushed onto the ValueStack
* in addition to the Action itself, allowing a FormBean type approach like Struts.
*
* @author Jason Carreira
*/
public interface ModelDriven<T> {
/**
* Gets the model to be pushed onto the ValueStack instead of the Action itself.
*
* @return the model
*/
T getModel();
}
只有一個泛型的getModel方法。所以只需要實現這個方法,返回一個實體對象就行。訪問路徑,和使用action的屬性訪問一樣。第二種使用的最多,但是個人比較喜歡第三種.