{"id":190,"date":"2007-09-06T00:00:00","date_gmt":"2007-09-06T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=190"},"modified":"2007-09-06T00:00:00","modified_gmt":"2007-09-06T00:00:00","slug":"Struts nested tag Example","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=190","title":{"rendered":"Struts nested tag Example"},"content":{"rendered":"<p><P><FONT face=\"Times New Roman\" color=#000080>The tag library \u5fdbested?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.&nbsp;<\/FONT><\/P><br \/>\n<P><FONT face=\"Times New Roman\" color=#000080>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. <\/FONT><\/P><br \/>\n<P><FONT face=\"Times New Roman\" color=#000080>As far as bean is concerned, one is associated with another internally and access to all the beans are&nbsp; 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 &#8216;parent&#8217; and &#8216;child&#8217; denotes a hierarchical structure of beans.&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>For example, Take an object which represents a Author. Each author&nbsp; related to many book&nbsp; .If this case was translated to bean objects,&nbsp;<BR>the author object would have a reference to the book objects he wrote, and each bunch object would hold a reference to<BR>the chapters in the books.<BR><BR>The author object is the parent to the books object, and the books object is a child of the author object. The books object<BR>is parent to its child chapters objects, and the child chapters objects children of the books object. The author is higher in<BR>the hierarchy than the books, and the chapters lower in the hierarchy to the books.<BR><\/FONT><\/P><br \/>\n<DIV><br \/>\n<P><FONT color=#000080><B>Herbert Schildt<BR>&nbsp;&nbsp;&nbsp;<\/B> Teach Yourself C++<BR>&nbsp;&nbsp;&nbsp; Java: The Complete Reference, J2SE<BR>&nbsp;&nbsp;&nbsp; Struts: the complete reference<BR><B>O&#8217;Reilly<BR>&nbsp;&nbsp;&nbsp; <\/B>.NET &amp; XML<BR>&nbsp;&nbsp;&nbsp; ADO.NET in a Nutshell<BR>&nbsp;&nbsp;&nbsp; ADO: ActiveX Data Objects<BR><BR>Create a new struts 1.1 project to to understand nested tags.<\/FONT> <\/P><br \/>\n<P><FONT color=#000080><B>Object Class Books<BR><\/B>Create a class <B>Books<\/B> with properties <B>id<\/B> and <B>name<\/B> in the package <B>roseindia.web.common<\/B>.<BR>Add a getter and setter method for each property.<BR>Also add&nbsp; a constructor that initialize the properties.<BR><BR><B>Books.java<\/B><\/FONT><\/P><br \/>\n<P><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"70%\" border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\" bgColor=#ffffcc><PRE>package roseindia.web.common;<\/PRE><PRE>public class Books {<\/p>\n<p>\tprivate int id;<br \/>\n\tprivate String name;<\/p>\n<p>\t\/\/constructors<br \/>\n\tpublic Books(){}<\/p>\n<p>\tpublic Books(int id, String name){<br \/>\n\t\tthis.id = id;<br \/>\n\t\tthis.name = name;<br \/>\n\t}<br \/>\n\tpublic int getId() {<br \/>\n\t\treturn id;<br \/>\n\t}<br \/>\n\tpublic void setId(int id) {<br \/>\n\t\tthis.id = id;<br \/>\n\t}<br \/>\n\tpublic String getName() {<br \/>\n\t\treturn name;<br \/>\n\t}<br \/>\n\tpublic void setName(String name) {<br \/>\n\t\tthis.name = name;<br \/>\n\t}<br \/>\n}<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><FONT color=#000080><B>Object Class Authors<\/B><BR>Create a second java class <B>Authors <\/B>in the same package <B>roseindia.web.common<\/B>.<BR>Add two properties, <B>id<\/B> of type int and <B>name<\/B> of type String and one property <B>books <\/B>of type Collection, which holds a list of <B>books<\/B>.<BR>Add a getter and setter method for each property.<BR>Also add&nbsp; a constructor that initialize the properties.<BR><BR><B>Authors.java<\/B><\/FONT><\/P><br \/>\n<P><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"70%\" border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\" bgColor=#ffffcc><PRE>package roseindia.web.common;<\/PRE><PRE>import java.util.*;<\/PRE><PRE>public class Authors {<br \/>\n\t<\/PRE><PRE>\tprivate int id;<br \/>\n\tprivate String name;<br \/>\n\t<\/PRE><PRE>\t\/\/books collection<br \/>\n\tprivate Collection books;<br \/>\n\t<\/PRE><PRE>\t\/\/constructors<br \/>\n\tpublic Authors() {}<br \/>\n\tpublic Authors(int id, String name, Collection books){<br \/>\n\t\tthis.id = id;<br \/>\n\t\tthis.name = name;<br \/>\n\t\tthis.books = books;<br \/>\n\t}<\/PRE><PRE>\tpublic Collection getBooks() {<br \/>\n\t\treturn books;<br \/>\n\t}<br \/>\n\tpublic void setBooks(Collection books) {<br \/>\n\t\tthis.books = books;<br \/>\n\t}<br \/>\n\tpublic int getId() {<br \/>\n\t\treturn id;<br \/>\n\t}<br \/>\n\tpublic void setId(int id) {<br \/>\n\t\tthis.id = id;<br \/>\n\t}<br \/>\n\tpublic String getName() {<br \/>\n\t\treturn name;<br \/>\n\t}<br \/>\n\tpublic void setName(String name) {<br \/>\n\t\tthis.name = name;<br \/>\n\t}<br \/>\n}<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><FONT color=#000080><B>Action form class AuthorsBooksForm<BR><\/B>Create a java class <B>AuthorsBooksForm <\/B>in the package <B>roseindia.web.struts.form<\/B>, which extends the class ActionForm of struts.<BR>Add a property <B>authors<\/B> of type <B>Authors<\/B> .<BR>Add a getter and setter method for the property <B>authors<\/B> .<BR><BR>Implement the reset() method of the ActionForm class.&nbsp;<BR><BR>AuthorsBooksForm.java<\/FONT><\/P><br \/>\n<P><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"75%\" border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\" bgColor=#ffffcc><PRE>package roseindia.web.struts.form;<\/PRE><PRE>import java.util.*;<\/PRE><PRE>import org.apache.struts.action.ActionForm;<br \/>\nimport org.apache.struts.action.ActionMapping;<br \/>\nimport javax.servlet.http.HttpServletRequest;<br \/>\nimport javax.servlet.http.HttpServletResponse;<br \/>\nimport roseindia.web.common.Authors;<br \/>\nimport roseindia.web.common.Books;<\/PRE><PRE>public class AuthorsBooksForm extends ActionForm {<\/PRE><PRE>\tAuthors authors;<\/PRE><PRE><br \/>\n\tpublic void setAuthors(Authors authors) {<br \/>\n\t\tthis.authors = authors;<br \/>\n\t}<\/PRE><PRE>\tpublic Authors getAuthors() {<br \/>\n\t\treturn authors;<br \/>\n\t}<br \/>\n\t<\/PRE><PRE>\tpublic void reset(ActionMapping mapping,HttpServletRequest request) {<br \/>\n\t\t<\/PRE><PRE>\t\t\/\/initial a dummy collection of books<br \/>\n\t\tCollection books = new ArrayList();<br \/>\n\t\t<\/PRE><PRE>\t\tbooks.add(new Books(1, &#8220;Teach Yourself C++&#8221;));<br \/>\n\t\tbooks.add(new Books(2, &#8220;Java: The Complete Reference, J2SE&#8221;));<br \/>\n\t\tbooks.add(new Books(3, &#8220;Struts: The Complete Reference&#8221;));<\/PRE><PRE><br \/>\n\t\t\/\/initial a dummy authors<br \/>\n\t\tauthors = new Authors(1, &#8220;Herbert Schildt&#8221;, books);<br \/>\n\t\t<\/PRE><PRE><br \/>\n\t}<br \/>\n}<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><FONT color=#000080><B>Action class AuthorsBooksAction<\/B>&nbsp;<BR><BR>Create a java class <B>AuthorsBooksAction<\/B> in the package <B>roseindia.web.struts.action<\/B>, which extends the class Action of struts.<BR>Return the forward <B>example<\/B>.<BR><BR><B>AuthorsBooksAction.java<\/B><\/FONT><\/P><br \/>\n<P><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"70%\" border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\" bgColor=#ffffcc><PRE>package roseindia.web.struts.action;<\/PRE><PRE>import roseindia.web.struts.form.AuthorsBooksForm;<\/PRE><PRE>import javax.servlet.http.HttpServletRequest;<br \/>\nimport javax.servlet.http.HttpServletResponse;<br \/>\nimport java.util.*;<br \/>\nimport org.apache.struts.action.Action;<br \/>\nimport org.apache.struts.action.ActionForm;<br \/>\nimport org.apache.struts.action.ActionForward;<br \/>\nimport org.apache.struts.action.ActionMapping;<\/p>\n<p><\/PRE><PRE>public class AuthorsBooksAction extends Action {<br \/>\n\t<\/PRE><PRE><br \/>\n\tpublic ActionForward execute(<br \/>\n\t\tActionMapping mapping,<br \/>\n\t\tActionForm form,<br \/>\n\t\tHttpServletRequest request,<br \/>\n\t\tHttpServletResponse response) {<br \/>\n\t\tAuthorsBooksForm booksForm = (AuthorsBooksForm) form;<br \/>\n\t\t<\/PRE><PRE>\t\treturn mapping.findForward(&#8220;books&#8221;);<br \/>\n\t}<\/PRE><PRE>}<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><B><FONT color=#000080>The JSP file<\/FONT><\/B><\/P><br \/>\n<P><FONT color=#000080>Create a new JSP file <B>authorsbooks.jsp<\/B>.<BR>Add the reference to the tag library nested at the top of the file.<\/FONT><\/P><br \/>\n<P><FONT color=#000080><B>authorsbooks.jsp<\/B><\/FONT><\/P><br \/>\n<P><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"80%\" border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\" bgColor=#ffffcc><PRE>&lt;%@ page language=&#8221;java&#8221;%&gt;<br \/>\n&lt;%@ taglib uri=&#8221;\/tags\/struts-bean&#8221; prefix=&#8221;bean&#8221; %&gt;<br \/>\n&lt;%@ taglib uri=&#8221;\/tags\/struts-html&#8221; prefix=&#8221;html&#8221; %&gt;<br \/>\n&lt;%@ taglib uri=&#8221;\/tags\/struts-logic&#8221; prefix=&#8221;logic&#8221; %&gt;<br \/>\n&lt;%@ taglib uri=&#8221;\/tags\/struts-nested&#8221; prefix=&#8221;nested&#8221;%&gt;<br \/>\n <\/PRE><PRE>&lt;html&gt;<br \/>\n\t&lt;head&gt;<br \/>\n\t\t&lt;title&gt;Struts nested tag Example&lt;\/title&gt;<br \/>\n\t&lt;\/head&gt;<br \/>\n\t&lt;body&gt;<br \/>\n\t  &lt;h1&gt;Struts nested tag Example&lt;\/h1&gt;<\/PRE><PRE>\t  &lt;b&gt;Author and his books:&lt;\/b&gt;<br \/>\n\t\t&lt;html:form action=&#8221;\/example&#8221; method=&#8221;post&#8221;&gt; <\/PRE><PRE><FONT color=#ff0000>\t<B>\t\t&lt;nested:nest property=&#8221;authors&#8221;&gt;<br \/>\n\t\t\t\t<\/B><\/FONT><\/PRE><PRE><FONT color=#ff0000><B>\t\t\t\t&lt;b&gt;&lt;nested:write property=&#8221;name&#8221;\/&gt; &lt;\/b&gt;<br \/>\n\t\t\t\t&lt;nested:iterate property=&#8221;books&#8221;&gt;<br \/>\n\t\t\t\t\t\t<\/B><\/FONT><\/PRE><PRE><FONT color=#ff0000><B>\t\t\t\t\t&lt;ul&gt;&lt;li&gt;&lt;nested:write property=&#8221;name&#8221;\/&gt;&lt;\/li&gt;&lt;\/ul&gt;<br \/>\n\t\t\t\t&lt;\/nested:iterate&gt;<br \/>\n\t\t\t&lt;\/nested:nest&gt;<\/B><\/FONT><br \/>\n\t\t\t<\/PRE><PRE>\t\t&lt;\/html:form&gt;<br \/>\n\t&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><B><FONT color=#000080>Configure the struts-config.xml<\/FONT><\/B> <\/P><br \/>\n<P><FONT color=#000080>Open the struts-config.xml and add the form bean and action mapping.<\/FONT> <\/P><br \/>\n<P><FONT color=#000080><B>struts-config.xml<\/B><BR><\/FONT><\/P><br \/>\n<P><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"70%\" border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\" bgColor=#ffffcc><PRE> &lt;form-beans&gt;<br \/>\n  \t&lt;form-bean name=&#8221;AuthorsBooksForm&#8221;<br \/>\n\t\t type=&#8221;roseindia.web.struts.form.AuthorsBooksForm&#8221; \/&gt;<br \/>\n &lt;\/form-beans&gt;<\/PRE><PRE>&lt;action-mappings&gt;<br \/>\n         &lt;action<br \/>\n            path=&#8221;\/example&#8221;<br \/>\n            type=&#8221;roseindia.web.struts.action.AuthorsBooksAction&#8221;<br \/>\n            input=&#8221;\/pages\/user\/authorsbooks.jsp&#8221;<br \/>\n            name=&#8221;AuthorsBooksForm&#8221;<br \/>\n            scope=&#8221;request&#8221;<br \/>\n            validate=&#8221;false&#8221;&gt;<br \/>\n            &lt;forward name=&#8221;example&#8221; path=&#8221;\/pages\/user\/authorsbooks.jsp&#8221; \/&gt;<br \/>\n        &lt;\/action&gt;<br \/>\n&lt;\/action-mappings&gt;<br \/>\n<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><FONT color=#000080><BR><B>Output:<BR><\/B>To view the output run the authorsbooks.jsp from the browser.<\/FONT><\/P><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tag library \u5fdbested?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.&nbsp; Nested tags are used in the nested context. The Nested tags &hellip; <a href=\"https:\/\/www.strongd.net\/?p=190\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Struts nested tag Example<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=190"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/190\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}