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.

你可能感兴趣的:(example)