1. 程式人生 > >struts標籤庫中ognl表示式獲取Action中物件的原理

struts標籤庫中ognl表示式獲取Action中物件的原理

在jsp頁面通過struts表單提交資料到Action中去,Action執行完畢返回jsp頁面通過struts標籤表示出值,都是通過Action類的setter和getter存取器,而存取器是根據jsp頁面的name值首字母大寫,加上get構造成的!

 <s:form action="User" method="post">
    <s:textfield name="<span style="color:#ff0000;">name</span>" label="姓名" required="true">
    </s:textfield>
    
    <s:password name="password" label="密碼" required="true">
    </s:password>
    
    <s:submit name="submit" value="登入"></s:submit>
    </s:form>
經過過濾器後,struts框架建立UserAction物件,並呼叫setName1(value)方法;
public class UserAction extends ActionSupport{
	private static final long serialVersionUID=1L;
	private String uname;
	private String password;
	
	public String <span style="color:#ff0000;">getUname</span>(){
		return uname;
	}
	public void <span style="color:#ff0000;">setUname</span>(String uname){
		this.uname=uname;
	}
	
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password=password;
	}
	public String execute(){
		return SUCCESS;
	}
}
結果自然是UserAction類無法初始化成功。

同樣,頁面中通過struts標籤和ognl表示式表示uname時也是一樣的

<s:property value="<span style="color:#ff0000;">name</span>"/> ,恭喜登陸成功!
這也無法正確獲取值,應為會呼叫getName() 方法,但是UserAction中並沒有該方法,所以,無法正常獲取值。

因此,這三處的變量表示應該一樣,同理,EL表示式也是類似情況。