例子:
//formBean定義:
public class PersonForm extends ActionForm {
Person person = new Person();
public void reset (ActionMapping mapping, HttpServletRequest request) {
Address[] address = new Address[2];
address[0]= new Address();
address[1]= new Address();
person = new Person();
person.setAddress(address);
}
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
}
//Person類:
public class Person {
private String lastName;
private String firstName;
private String age;
private Address address;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return age;
}
public void setAddress(Address address){
this.address = address;
}
public Address getAddress(){
return address;
}
}
//Address類:
public class Address {
private String street1;
private String street2;
public String getStreet1() {
return street1;
}
public void setStreet1(String street1) {
this.street1 = street1;
}
public void setStreet2(String street2) {
this.street2 = street2;
}
public String getStreet2() {
return street2;
}
}
//在jsp中使用標籤
<html:form action="/showPerson">
<nested:nest property="person">
Last Name: <nested:text property="lastName"/><BR>
First Name: <nested:text property="firstName"/><BR>
Age: <nested:text property="age"/><BR>
P>
<nested:iterate id="child" name="person" property="address" type="nestedtaglibs.Address">
Current nesting is: <nested:writeNesting/><BR>
Street 1: <nested:text name="child" property="street1"/><BR>
Street 2: <nested:text name="child" property="street2"/><BR>
</nested:iterate >
</nested:nest>
<html:submit/>
</html:form>
詳解:
Struts Nested標籤庫的一部分標籤用於表達JavaBean之間的嵌套關係,還有一部分標籤能夠在特定的嵌套級別提供和其他Struts標籤相同的功能。
- <nested:nest>,定義一個新的嵌套級別
- <nested:writeNesting>,輸出當前嵌套級別信息
<nested:nest>標籤可以表達JavaBean之間的嵌套關係,以三個JavaBean爲例,分別是:PersonForm Bean,Person Bean和Address Bean,在PersonForm Bean中包含一個Person Bean類型的屬性person,在Person Bean中又包含一個Address Bean類型的屬性address。
定義兩個<nested:nest>標籤,第一個<nested:nest>標籤嵌套在<html:form>標籤中,如下:
<html:form action="/showPerson">
<nested:nest property="person">
Last Name:<nested:text property="lastName"/><BR>
.....
</nested:nest>
</html:form>
以上<nested:nest>標籤的上層JavaBean爲於<html:form>表單標籤對應的PersonForm Bean,<nested:nest>標籤的property屬性爲“person",代表PersonForm Bean的person屬性,這個person屬性代表Person Bean,因此嵌套在<nested:nest>標籤內部的Nested標籤都相對於這個Person Bean,例如第一個<nested:text>標籤的property屬性”lastName“,代表Person Bean的lastName屬性。
第二個<nested:nest>標籤嵌套在第一個<nested:nest>標籤內部:如下
<html:form action="/showPerson">
<nested:nest property="person">
.............
<nested:nest property="address">
Current nesting is :<nested:writeNesting/><br>
Street 1:<nested:text property="street1"/><BR>
.....
</nested:nest>
</nested:nest>
</html:form>
在以上代碼中,第二個<nested:nest>標籤的property屬性爲“address",代表PersonBean 的address屬性,這個address屬性代表Address Bean,因此嵌套在第二個<nested:nest>標籤內部的Nested標籤都相對於這個Address Bean。
第二個<nested:nest>標籤內還嵌套了一個<nested:writeNesting>標籤,它顯示當前的嵌套級別,輸出結果爲”person.address".
在默認情況下,<nested:nest>標籤的property屬性爲當前ActionForm Bean的某個屬性,或者爲於上層<nested:nest>標籤對應的JavaBean的某個屬性,可以使用<nested:root>標籤來顯式指定頂層級別的JavaBean,<nested:root>標籤的name屬性指定JavaBean的名字,嵌套在<nested:root>標籤中的<nested:nest>標籤的property屬性爲這個JavaBean的某個屬性。
和其他標籤庫中的標籤功能相同的Nested標籤
許多Nestd標籤庫中的標籤具有和其他標籤庫中的標籤相同的功能,區別在於Nested標籤庫中的標籤屬性相對於當前的嵌套級別,例如
<nested:nest property = "person">
Last name :<nested:text property="lastName"/>
</nested:nest>
上面的<nested:text>標籤和<html:text>標籤具有相同的功能,都可以生成文本框,兩者的區別在於<nested:text>標籤的property屬性爲於當前嵌套級別對應的JavaBean的某個屬性,而<html:text>標籤的property屬性爲於當前表單對應的ActionForm Bean的某個屬性。
====================================
比如我有一個User類和一個UserInfo類,前者記錄用戶的帳號密碼,後者記錄用戶的詳細信息。前者也有一個UserInfo屬性,這樣它們兩者是嵌套了。
現在我要把這個用戶的帳號和詳細信息都顯示到界面上。
一種方式是在actionForm中用兩個屬性User user和UserInfo userInfo來存儲,在jsp中就可以用如下方式顯示出來:
<nested:nest property="user">
帳號:<nested:write property="account"/>
</nested:nest>
<nested:nest property="userInfo">
姓名:<nested:write property="name"/>
性別:<nested:write property="sex"/>
</nested:nest>
由於user和userInfo本身就是嵌套的,所以第二種方式就在actionForm中使用一個User user屬性即可:
<nested:nest property="user">
帳號:<nested:write property="account"/>
<nested:nest property="userInfo">
姓名:<nested:write property="name"/>
性別:<nested:write property="sex"/>
</nested:nest>
</nested:nest>
這樣處理是不是很方便了,actionForm可以直接放上數據存儲對象,如果使用了hibernate做數據持久層,我們就可以直接把持久畫對象放入actionForm來顯示到界面上,不用在actionForm裏寫很多屬性來分別存儲數據,也免去了給這些屬性分別賦值的過程。
如果我們把上邊例子中的<nested:write/>標記換成<nested:text/>,這就類似於<html:text/>標記,是一個輸入框,這樣我們就可以把街面上輸入一次提交到actionForm中的這個數據存儲對象,比如user。我們在action中就可以直接獲得這個user進行處理,非常方便。