Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Struts nested tag Example

The tag library ?nested? 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.

Ads

Tutorials   
Struts 2.5.10.1 General Availability - released Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 version 2.3.16.3 released Struts 2 version 2.3.15.3 released Struts 2 Hello World Annotation Example Struts Roseindia Struts 2 Tutorial: Easy steps to learn Struts 2 Difference between Struts and Spring Apache Struts 2.3.15.2 launched to fix vulnerable Feature Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts 1 Tutorial and example programs Struts Tutorial What is Struts Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts Video Tutorials Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 In Struts What is Model? What is Struts? Download Struts What is Struts Framework? Struts Framework Latest Version of Struts Framework Downloading Struts Official example Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Hibernate4 Struts Integration Struts2 Tiles Example
Ads

Struts nested tag Example

     

The tag library ?nested? 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 {
	
	private int id;
	private String name;
	
	//constructors
	public Books(){}

	public Books(int id, String name){
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

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;
	private String name;
	
	//books collection
	private Collection books;
	
	//constructors
	public Authors() {}
	public Authors(int id, String name, Collection books){
		this.id = id;
		this.name = name;
		this.books = books;
	}
	public Collection getBooks() {
		return books;
	}
	public void setBooks(Collection books) {
		this.books = books;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

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;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import roseindia.web.common.Authors;
import roseindia.web.common.Books;
public class AuthorsBooksForm extends ActionForm {
	Authors authors;
				
	public void setAuthors(Authors authors) {
		this.authors = authors;
	}
	public Authors getAuthors() {
		return authors;
	}
	
public void reset(ActionMapping mapping,HttpServletRequest request) {
		
	//initial a dummy collection of books
	Collection books = new ArrayList();
		
	books.add(new Books(1, "Teach Yourself C++"));
	books.add(new Books(2, "Java: The Complete Reference, J2SE"));
	books.add(new Books(3, "Struts: The Complete Reference"));
       
	//initial a dummy authors
	authors = new Authors(1, "Herbert Schildt", books);
		
		
	}
}

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;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AuthorsBooksAction extends Action {
	
	
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		AuthorsBooksForm booksForm = (AuthorsBooksForm) form;
		
		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"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
 
<html> 
	<head>
		<title>Struts nested tag Example</title>
	</head>
	<body>
	  <h1>Struts nested tag Example</h1>
	  <b>Author and his books:</b>
		<html:form action="/example" method="post"> 
			<nested:nest property="authors">
				
	<b><nested:write property="name"/> </b>
	<nested:iterate property="books">
						
<ul><li><nested:write property="name"/></li></ul> </nested:iterate> </nested:nest>
		</html:form> 
	</body>
</html>

Configure the struts-config.xml

Open the struts-config.xml and add the form bean and action mapping.

struts-config.xml

 <form-beans>
  	<form-bean name="AuthorsBooksForm"
		 type="roseindia.web.struts.form.AuthorsBooksForm" /> 
 </form-beans>
<action-mappings>
         <action
            path="/example"
            type="roseindia.web.struts.action.AuthorsBooksAction"
            input="/pages/user/authorsbooks.jsp"
            name="AuthorsBooksForm"
            scope="request"
            validate="false">
            <forward name="example" path="/pages/user/authorsbooks.jsp" />
        </action>
</action-mappings>


Output:
To view the output run the authorsbooks.jsp from the browser.

     

Advertisements

Online Training Classes

Share on Google+Share on Google+

Struts nested tag Example

Posted on: February 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Struts nested tag Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads