Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.

 

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.

                         

» View all related tutorials
Related Tags: c programming apache web com development asp ui framework build application io components make sed get ip hook vi concepts

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

7 comments so far (
post your own) View All Comments Latest 10 Comments:

java.lang.ClassNotFoundException: org.apache.struts.taglib.nested.NestedWriteNestingTei

Hi all,

The reason for this ugly stack is because
the of incompatibility between struts.jar
and TAG library definition.

Your TAG library version is 1.2
but your struts.jar Specification-Version: 1.1

The tomact by going through your struts-nested.tld
of version 1.2 figured out that it has to load
"org.apache.struts.taglib.nested.NestedWriteNestingTei"
which is not there in struts.jar of version 1.1

Good Luck :)

Posted by Sudhakar KV on Thursday, 06.5.08 @ 16:29pm | #62188

Very nice tutorial! It has been very helpful.

Posted by Nikos on Wednesday, 11.21.07 @ 17:03pm | #38175

please restart ur server, check ur jsp contaier,library finles

Posted by nani on Wednesday, 05.30.07 @ 15:54pm | #17730

This is good one, but an example ofsome runtime data entry from the user side should be there...

Posted by Indubhusan on Friday, 04.13.07 @ 09:16am | #14042

i try to run the above program.but the following error is occured

please reply to my mail

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Failed to load or instantiate TagExtraInfo class: org.apache.struts.taglib.nested.NestedWriteNestingTei
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:279)
org.apache.jasper.compiler.TagLibraryInfoImpl.createTagInfo(TagLibraryInfoImpl.java:422)
org.apache.jasper.compiler.TagLibraryInfoImpl.parseTLD(TagLibraryInfoImpl.java:248)
org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:162)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:418)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:483)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1543)
org.apache.jasper.compiler.Parser.parse(Parser.java:126)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:146)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.ClassNotFoundException: org.apache.struts.taglib.nested.NestedWriteNestingTei
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1332)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
org.apache.jasper.compiler.TagLibraryInfoImpl.createTagInfo(TagLibraryInfoImpl.java:419)
org.apache.jasper.compiler.TagLibraryInfoImpl.parseTLD(TagLibraryInfoImpl.java:248)
org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:162)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:418)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:483)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1543)
org.apache.jasper.compiler.Parser.parse(Parser.java:126)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:146)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.9

Posted by kokul on Saturday, 03.10.07 @ 11:03am | #11304

this tutorils is very nice for a beginer,who have some litle knowledge in struts,the author has done a great job.......

Posted by ravishankar on Wednesday, 03.7.07 @ 14:25pm | #10973

how to use nested tags with dynaactionforms .
can you send me a tutorial for the same

Posted by vidyadhar on Wednesday, 02.28.07 @ 13:04pm | #9939

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.