The tag library 忛ested?is included in Struts 1.1. In this tutorial we are going to explain what are the features of nested tag library and how you can use it. We can manage nested beans easily with the help of struts nested tag library.
Nested tags are used in the nested context. The Nested tags and its supporting classes extends the base struts tags and making them possible to relate to each other in the nested fashion. In case of Nested tags the original logic of the tags does not change and all the references to beans and bean properties is managed in the nested context.
As far as bean is concerned, one is associated with another internally and access to all the beans are through the current one. The beans that refers to another is the parent and the second related to the first one is its child. Here both ‘parent’ and ‘child’ denotes a hierarchical structure of beans.
For example, Take an object which represents a Author. Each author related to many book .If this case was translated to bean objects,
the author object would have a reference to the book objects he wrote, and each bunch object would hold a reference to
the chapters in the books.
The author object is the parent to the books object, and the books object is a child of the author object. The books object
is parent to its child chapters objects, and the child chapters objects children of the books object. The author is higher in
the hierarchy than the books, and the chapters lower in the hierarchy to the books.
Herbert Schildt
Teach Yourself C++
Java: The Complete Reference, J2SE
Struts: the complete reference
O’Reilly
.NET & XML
ADO.NET in a Nutshell
ADO: ActiveX Data Objects
Create a new struts 1.1 project to to understand nested tags.
Object Class Books
Create a class Books with properties id and name in the package roseindia.web.common.
Add a getter and setter method for each property.
Also add a constructor that initialize the properties.
Books.java
package roseindia.web.common; public class Books { |
Object Class Authors
Create a second java class Authors in the same package roseindia.web.common.
Add two properties, id of type int and name of type String and one property books of type Collection, which holds a list of books.
Add a getter and setter method for each property.
Also add a constructor that initialize the properties.
Authors.java
package roseindia.web.common; import java.util.*; public class Authors { private int id; //books collection //constructors public Collection getBooks() { |
Action form class AuthorsBooksForm
Create a java class AuthorsBooksForm in the package roseindia.web.struts.form, which extends the class ActionForm of struts.
Add a property authors of type Authors .
Add a getter and setter method for the property authors .
Implement the reset() method of the ActionForm class.
AuthorsBooksForm.java
package roseindia.web.struts.form; import java.util.*; import org.apache.struts.action.ActionForm; public class AuthorsBooksForm extends ActionForm { Authors authors;
public Authors getAuthors() { public void reset(ActionMapping mapping,HttpServletRequest request) { //initial a dummy collection of books books.add(new Books(1, “Teach Yourself C++”));
|
Action class AuthorsBooksAction
Create a java class AuthorsBooksAction in the package roseindia.web.struts.action, which extends the class Action of struts.
Return the forward example.
AuthorsBooksAction.java
package roseindia.web.struts.action; import roseindia.web.struts.form.AuthorsBooksForm; import javax.servlet.http.HttpServletRequest; public class AuthorsBooksAction extends Action {
return mapping.findForward(“books”); } |
The JSP file
Create a new JSP file authorsbooks.jsp.
Add the reference to the tag library nested at the top of the file.
authorsbooks.jsp
<%@ page language=”java”%> <html> <b>Author and his books:</b> <nested:nest property=”authors”> <b><nested:write property=”name”/> </b> <ul><li><nested:write property=”name”/></li></ul> </html:form> |
Configure the struts-config.xml
Open the struts-config.xml and add the form bean and action mapping.
struts-config.xml
<form-beans> <action-mappings> |
Output:
To view the output run the authorsbooks.jsp from the browser.