How to use Nested Tags in Struts

Here I am explaining how to use Nested Tags in Struts .

College.java

package com.Nested;

import java.util.*;

public class College {

private int id;
private String name;

private Collection Students;

public College() {}
public College(int id, String name, Collection students){
this.id = id;
this.name = name;
this.Students = students;
}
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;
}
public Collection getStudents() {
return Students;
}
public void setStudents(Collection students) {
Students = students;
}

}

NestedAction.java

package com.Nested;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 NestedAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

NestedForm booksForm = (NestedForm) form;
return mapping.findForward("nested");
}

}

NestedForm.java

package com.Nested;

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;

public class NestedForm extends ActionForm {

College college;
public College getCollege() {
return college;
}
public void setCollege(College college) {
this.college = college;
}
Students student;

public Students getStudent() {
return student;
}
public void setStudent(Students student) {
this.student = student;
}
public void reset(ActionMapping mapping,HttpServletRequest request) {

Collection Students = new ArrayList();
Students.add(new Students("1", "Santhi","MCA"));
Students.add(new Students("2", "Prasanthi","MBA"));
Students.add(new Students("3", "Madhuri","MBA"));
Students.add(new Students("4", "Harini","MBA"));
Students.add(new Students("5", "Shalini","MCA"));
college = new College(1, "GIPS", Students);

}
}

Students.java

package com.Nested;

/**
* @author Prasanthi
*
*/
public class Students {

private String id;
private String name;
private String course;

public Students(){}

public Students(String id, String name,String course){
this.id = id;
this.name = name;
this.course=course;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}
}

NestedTest.jsp

<%@ page language="java"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>

<html>
<body>
<br><br>
<center>
<h2><u>Nested Example</u></h2>
<html:form action="/nest" method="post">

<nested:nest property="college">
<b>
College Name:<nested:write property="name"/></b><br>
<h3><u>Students Table</u></h3>
<table border="1" cellpadding="1" cellspacing="2" width="40%">
<tr><td align="center">ID</td>
<td align="center">Name</td>
<td align="center">Course</td>
</tr>
<nested:iterate property="students">

<tr> <td align="center"><nested:write property="id"/></td>
<td align="center"><nested:write property="name"/></td>
<td align="center"><nested:write property="course"/></td>
</tr>
</nested:iterate>
</table>
</nested:nest>

</html:form>
</body>
</html>

web.xml

<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>NestedTest.jsp</welcome-file>
</welcome-file-list>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
</web-app>

struts-config.xml

<struts-config>

<form-beans>
<form-bean name="Nestedform" type="com.Nested.NestedForm" />
</form-beans>

<action-mappings>
<action
path="/nest"
type="com.Nested.NestedAction"
input="/NestedTest.jsp"
name="Nestedform"
scope="request"
validate="false">
<forward name="nested" path="NestedTest.jsp" />
</action>
</action-mappings>

</struts-config>

No comments: