Using the Struts Nested Tags


This section provides information about the following tags:

  • <nested:nest> ” Define a new level of nesting

  • <nested:root> ” Define the root level of a nested hierarchy

  • <nested:writeNesting> ” Send the current nesting level to the page

By using the <nested:nest> tag, you define the object relative to which all contained tags do their work.

The best way to demonstrate this is with an example. Examine the JSP shown in Listing 15.1.

Listing 15.1 JSP File Demonstrating Use of Struts Nested Tags ( Nested.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %> <%@ page import="java.util.Vector" %> <%@ page import="ch15.Person" %> <%@ page import="ch15.Address" %> <html:html> <head> <title>Misc Nested sample code</title> </head> <body bgcolor="white"> <h3>Misc Nested sample code</h3> <p>This page provides examples of the following Struts NESTED tags:<br> <ul> <li>&lt;nested:nest&gt;</li> <li>&lt;nested:select&gt;</li> <li>&lt;nested:text&gt;</li> <li>&lt;nested:writeNesting&gt;</li> </ul> <%-- The following section shows nest. --%> <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> Gender: <nested:select property="gender"> <html:option value="MALE">Male</html:option> <html:option value="FEMALE">Female</html:option> </nested:select><P> <nested:nest property="address"> Current nesting is: <nested:writeNesting/><BR> Street 1: <nested:text property="street1"/><BR> Street 2: <nested:text property="street2"/><BR> City: <nested:text property="city"/><BR> State: <nested:text property="state"/><BR> Postal Code: <nested:text property="postalCode"/><BR> </nested:nest> </nested:nest> <html:submit/> </html:form> </html:body> </html:html> 

This page implements a form in which some of the elements of the form are contained in one bean and some of the elements are contained in another bean that's a property of the first bean. In this case, the Person bean has a property called address , which itself contains another bean, the Address bean.

To understand this page better, you need to look at the definition of the Person and Address objects shown in Listings 15.2 and 15.3, respectively.

Listing 15.2 Java Source for Person Object ( Person.java )
 package ch15; public class Person {   private String lastName;   private String firstName;   private String age;   private String gender;   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 setGender(String gender) {     this.gender = gender;   }   public String getGender() {     return gender;   }   public void setAddress(Address address) {     this.address = address;   }   public Address getAddress() {     return address;   } } 

This is the same old bean format that should be old hat to you by now. The only thing to notice is that the address property contains another bean rather than a String .

Listing 15.3 Source for Address Object ( Address.java )
 package ch15; public class Address {   private String street1;   private String street2;   private String city;   private String state;   private String postalCode;   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;   }   public void setCity(String city) {     this.city = city;   }   public String getCity() {     return city;   }   public void setState(String state) {     this.state = state;   }   public String getState() {     return state;   }   public void setPostalCode(String postalCode) {     this.postalCode = postalCode;   }   public String getPostalCode() {     return postalCode;   } } 

As you can see, the Person object defines the familiar list of attributes you'd associate with a person, such as last name and first name, as well as a pointer to a secondary Address object that stores the mailing address of the person.

The Struts configuration entry for the showPerson action is defined as

 <action   path="/showPerson" name="NestedForm" type="ch15.NestedAction"           validate="false" scope="request">   <forward name="default"               path="/showPerson.jsp"/> </action> 

This depends on the NestedForm form, defined as

 <form-bean name="NestedForm" type="ch15.PersonForm"/> 

Finally, PersonForm is defined in Listing 15.4.

Listing 15.4 Source for PersonForm ActionForm ( PersonForm.java )
 package ch15; import org.apache.struts.action.ActionForm; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionMapping; public class PersonForm extends ActionForm {   Person person = new Person();   public void reset (ActionMapping mapping, HttpServletRequest request) {     person = new Person();     person.setAddress(new Address());   }   public Person getPerson() {     return this.person;   }   public void setPerson(Person person) {     this.person = person;   } } 

With all this in place, you're ready to take a walk through the code. Going back to the JSP page, the action of the form specifies showPerson , which is looked up in the Struts configuration file. Struts determines that this action uses the NestedForm form, which is of type ch15.PersonForm . Struts then checks to make sure that this form doesn't already exist in the requested scope (by default, request ), and because it doesn't, Struts creates a new form. As part of that creation, it calls the reset method, which causes the form to create a Person object and an Address object, and store the Address inside the Person .

Note that in this example, rather than the ActionForm containing individual bean properties for each field, it contains a single property that holds an object and the object has slots for all the fields.

Next, return to the JSP page. The next directive after the <html:form> tag is <nested:nest> . This tag directs that any reference made using a tag from the nested taglib be relative to the specified form property.

Following that, you see a series of fields using the <nested:text> tag. In fact, a large number of tags in the Nested library are simple extensions of their analogs in another taglib. The full list is as follows :

  • checkbox

  • define

  • empty

  • equal

  • errors

  • file

  • form

  • greaterEqual

  • greaterThan

  • hidden

  • image

  • img

  • iterate

  • lessEqual

  • lessThan

  • link

  • match

  • message

  • messages

  • messagesNotPresent

  • messagesPresent

  • multibox

  • notEmpty

  • notEqual

  • notMatch

  • notPresent

  • options

  • optionsCollection

  • password

  • present

  • radio

  • select

  • size

  • submit

  • text

  • textarea

  • write

If you use any of these tags with the nested prefix rather than its native prefix ( <nested:text> instead of <html:text> , for example), it works exactly the same as normal, except that the property= value has the current nesting value added to the front. That means when the first <nested:text> tag refers to lastName , it's really talking about person.lastName .

Later in the form, the <nested:nest> tag is used again, this time to fill in the values for the address, which is a child object of Person . By using another <nested:nest> inside the outer one, you create a reference to a subobject. In other words, nesting can be recursive.

The <nested:writeNesting> tag confirms this by displaying the nesting level as person.address . Figure 15.2 shows this page in action.

Figure 15.2. The first page of the nesting example at /Taglib/Nested.do .

graphics/15fig02.gif

When the form is submitted, control is handed to showPerson.jsp (shown in Listing 15.5).

Listing 15.5 Displaying the Submitted Nested Values ( showPerson.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %> <%@ page import="java.util.Vector" %> <%@ page import="ch15.Person" %> <%@ page import="ch15.Address" %> <html:html> <head> <title>Misc NESTED sample code</title> </head> <body bgcolor="white"> <h3>Misc NESTED sample code</h3> <p>This page provides examples of the following Struts NESTED tags:<br> <ul> <li>&lt;nested:nest&gt;</li> <li>&lt;nested:root&gt;</li> <li>&lt;nested:write&gt;</li> </ul> <%-- The following section shows nest. --%> <jsp:useBean id="NestedForm" type="ch15.PersonForm" scope="request"/> <nested:root name="NestedForm"> <nested:nest property="person"> Last Name: <nested:write property="lastName"/><BR> First Name: <nested:write property="firstName"/><BR> Age: <nested:write property="age"/><BR> Gender: <nested:write property="gender"/><P> <nested:nest property="address"> Street 1: <nested:write property="street1"/><BR> Street 2: <nested:write property="street2"/><BR> City: <nested:write property="city"/><BR> State: <nested:write property="state"/><BR> Postal Code: <nested:write property="postalCode"/><BR> </nested:nest> </nested:nest> </nested:root> </html:body> </html:html> 

This page shows one more useful tag: <nested:root> . By default, the <nested:nest> tag assumes that a form has been set up, and uses that form to look up the properties to nest on. The <nested:root> tag enables you to explicitly define the bean to be nested on. In this case, it's the submitted form. Figure 15.3 shows this page after submission.

Figure 15.3. The submitted form at /Taglib/showPerson.do .

graphics/15fig03.gif



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net