`
oywl2008
  • 浏览: 1051110 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    struts2标签和解释及其说明

    struts2标签和解释及其说明 嘿嘿其实就是那个下载人数最多的那个的东西..他要10分啊..太心黑了..呵呵我借鉴哈和你们一起分享哈!但是我花了10分不能不挣回来吧!大家一人补偿我点就好了!.......一起学习!

    struts-hibernate-nested-demo学习实例

    这个"struts-hibernate-nested-demo"学习实例旨在帮助开发者了解如何将这两个框架结合使用,以构建更复杂的Web应用程序。 在Struts框架中,MVC模式被用来分离业务逻辑、视图和控制流程。控制器接收用户请求,调用...

    jsoup解析xml文件

    **jsoup解析XML文件** ...这个库提供了简洁的API,使得提取和操作网页数据变得异常简便。在给定的资源中,我们可以看到一个使用jsoup解析XML文件的简单示例。 首先,我们来理解jsoup的基本用法。...

    struts-html.tld

    struts-html.tld

    Spring Nested事务简单案例

    在本案例中,我们关注的是Spring中的Nested事务,这是一个相对复杂的特性,但非常有用,特别是在需要子事务处理的场景下。下面将详细解释这个概念。 Nested事务是基于JDBC的Savepoint机制实现的,它可以让我们在一...

    struts标签 struts标签 struts标签

    Struts标签是Apache Jakarta Struts框架中的一部分,用于简化Java Web应用程序开发中的视图层构建。Struts标签库提供了一组预定义的JSP标签,这些标签有助于开发者更有效地处理常见的Web开发任务,如表单处理、数据...

    Struts基本知识培训

    Struts提供了丰富的标签库,如html、bean、logic和nested标签,这些标签简化了JSP页面的编写,使代码更清晰,降低了视图层与业务逻辑的耦合。 **基于STRUTS的WEB应用程序介绍** 例如,WROX-STRUTS例子是一个经典的...

    struts1 jar包

    5. `struts-nested.jar`:支持嵌套表单和标签,使得在复杂视图中操作更方便。 6. `struts-el.jar`:支持Expression Language,允许在JSP中使用更灵活的数据绑定。 7. `commons-logging.jar`:Apache Commons ...

    struts-hibernate-nested-demo.rar_DEMO_JSTL de_java 主从表_从表

    Struts-Hibernate-Nested-Demo 是一个基于Java的Web应用程序示例,主要展示了如何使用Struts框架、JSTL(JavaServer Pages Standard Tag Library)以及Hibernate ORM(对象关系映射)来处理数据库中的主从表关系。...

    Struts2核心标签注解

    本文将深入解析Struts2中的核心标签,包括HTML Tag、Bean Tag、Logic Tag、Nested Tag以及Template Tag,并探讨它们在实际开发中的应用。 ### 1. HTML Tag HTML Tag主要用于创建能够与Struts框架及其它相关HTML...

    struts1-tlds.rar

    c-rd.tld, c.tld, fmt-rt.tld, fmt.tld, permittedTaglibs.tld, scriptfree.tld, sql-rt.tld, sql.tld, struts-bean-el.tld, ...struts-nested.tld, struts-tiles-el.tld, struts-tiles.tld, x-rt.tld, x.tld

    struts框架与标签.doc

    Struts 1.1引入了Nested Tag,使得在JSP中处理嵌套对象变得更加简单。随着时间的推移,Struts框架不断演进,后续版本增加了更多功能和改进,如Struts 2引入了更强大的标签库和拦截器机制,以及更灵活的配置方式。 ...

    Struts 1.2.9 jar 包及原文件

    5. **struts-nested.jar**: 支持嵌套表单和HTML元素,这在处理复杂的表单结构时非常有用。 6. **其他依赖的jar包**:例如commons-lang.jar、commons-logging.jar、ognl.jar等,它们分别提供了Apache Commons的实用...

    struts-1.2.9-lib.zip和struts-1.2.9-src.zip

    2. **标签库**:包括`struts-bean.jar`、`struts-html.jar`、`struts-logic.jar`和`struts-nested.jar`等,提供了一系列预定义的JSP标签,如logic标签用于流程控制,bean标签用于展示数据,html标签则用于表单元素的...

    精通Struts书中讲解nestedtaglibs的例子

    在"精通Struts"这本书中,nestedtaglibs是一个重要的章节,专门讲解了如何使用Struts的内嵌标签库(Nested Tag Libraries)来更有效地构建动态Web界面。Nested Tag Libraries是Struts提供的一个增强用户界面展示能力...

    JSTL的各种标签文件和struts的标签文件,带strtus.jar包

    JSTL的各种标签文件和struts的标签文件,带strtus.jar包c-1_0.tld,c-1_0-rt.tld,c.tld,fmt-1_0.tld,fmt-1_0-rt.tld,fmt.tld,fn.tld,permittedTaglibs....struts-bean.tld,struts-nested.tld,struts.jar

    struts1jar大全

    6. **struts-nested.jar**:支持嵌套属性的展现,对于处理复杂的表单和对象层次很有帮助。 7. **xwork.jar**:Struts1后期版本引入的Action框架基础,用于处理Action的执行逻辑。 8. **ognl.jar**:Object-Graph ...

    vscode-nested-tags:停止使用文件树来组织笔记。 通过添加标签支持将VSCode转换为笔记应用程序

    -- @nested-tags:topic,here/is/a/nested/example --&gt; 或带有标签属性的yaml frontmatter(方括号样式) --- title : Hello nested tags tags : [topic, here/is/a/nested/example] --- 或带有标签属性的yaml ...

    struts体系结构与工作原理

    Struts 有一组相互协作的类(组件)、Servlet 以及 JSP Tag Lib 组成。基于 Struts 架构的 Web 应用程序基本上符合 JSP Model2 的设计标准,可以说是一个传统 MVC 设计模式的一种变化类型。 Struts 的体系结构与...

Global site tag (gtag.js) - Google Analytics