2011-07-14 16 views

回答

26

Javadoc文檔<bean:write>

指定其屬性的訪問,以 檢索由財產指定值的bean的屬性名稱(如果指定)。如果未指定屬性 ,則會顯示此bean本身的值。

從本質上說,如果你有一個JavaBean(具有getter和setter),

Person person = new Person; 
request.setAttribute("person", person); 

通過設置<bean:write name="person" property="age" />,你告訴Struts來先找到person對象第一PageContext範圍。如果沒有找到,則request,然後session,然後application範圍。

property="age"屬性(從<bean:write />標記),然後將調用getter方法getAge()Person對象(不論是否有稱爲age對bean的實例變量)。

希望這會有所幫助。

+3

+1爲迷惑了一下範圍查找順序詳細解釋 -1:這是'page','request','session'然後'application' – Vlad

+0

@Vlad,感謝發現我的錯誤。我已編輯修復錯誤。 –

+0

謝謝解釋:) +1解釋 – Arun

3

爲了顯示person.getAge()你會用

<bean:write name="person" property="age" /> 
0

「name」屬性應指定bean的名稱。例如,如果您試圖從ActionForm輸出屬性,則應將name屬性設置爲ActionForm的名稱,並將屬性屬性設置爲您要寫入的ActionForm的屬性。因此,在這種情況下,你可能會做:

<bean:write name="productInfo" property="summary" /> 

如果聲明例如使用標籤的非的ActionForm Bean,那麼name屬性將被設置爲定義的bean的名字:

<bean:define id="displayText" value="Text to Display" /> 
<bean:write name="displayText" /> 

請注意,在這種情況下屬性屬性丟失,在這種情況下,將顯示bean本身的tostring值。

相關問題