3:09 pm - Monday October 21, 0480

Struts logic:iterate example program

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

This tutorial explains how to iterate the data or object present in an collection using struts-logic.tld file, logic iterate will iterate all the data present in ArrayList object and bean write will print the required data in the front end.

Project Structure:

JAR FILES: 

TLD FILES: 

 

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

PersonAction.java:

package com.candid;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class PersonAction extends org.apache.struts.action.Action {
	private static final String SUCCESS = "success";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersonForm personForm = (PersonForm) form;
		PersonDetail detail = new PersonDetail();
		personForm.setList(detail.InsertData());
		return mapping.findForward(SUCCESS);

	}

}

PersonDetail.java:

package com.candid;

import java.util.ArrayList;

public class PersonDetail {

	private static final long serialVersionUID = 1L;
	private String name;
	private String city;

	public PersonDetail() {

	}

	public PersonDetail(String name, String city) {
		// TODO Auto-generated constructor stub

		this.name = name;
		this.city = city;
	}

	public ArrayList InsertData() {
		ArrayList list = new ArrayList();
		list.add(new PersonDetail("Anbu", "cuddalore"));
		list.add(new PersonDetail("Bala", "chennai"));
		list.add(new PersonDetail("Guna", "chennai"));
		list.add(new PersonDetail("Madhavan", "madurai"));

		return list;
	}

	public String getName() {
		return name;
	}

	public String getCity() {
		return city;
	}

}

PersonForm.java:

package com.candid;

import java.util.ArrayList;

public class PersonForm extends org.apache.struts.action.ActionForm {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList list;

	public ArrayList getList() {
		return list;
	}

	public void setList(ArrayList list) {
		this.list = list;
	}

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<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>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</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>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>welcome.jsp</welcome-file>
	</welcome-file-list>
	<jsp-config>
		<taglib>
			<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
		</taglib>
	</jsp-config>
</web-app>

struts-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="PersonForm" type="com.candid.PersonForm" />

	</form-beans>

	<action-mappings>
		<action input="/welcome.jsp" name="PersonForm" path="/Action"
			scope="session" type="com.candid.PersonAction">
			<forward name="success" path="/success.jsp" />
		</action>
	</action-mappings>

</struts-config>

success.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success</title>
</head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Member Details</title>
</head>
<body bgcolor="pink">
<table cellpadding="3" cellspacing="3" border="1">
	<tr>
		<th>Name</th>
		<th>City</th>

	</tr>
	<logic:iterate id="data" name="PersonForm" property="list">
		<tr>
			<td><bean:write name="data" property="name" /></td>
			<td><bean:write name="data" property="city" /></td>

		</tr>
	</logic:iterate>
</table>
</body>

</html>

test.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
TEST
</body>
</html>

welcome.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body bgcolor="pink">
<html:form action="/Action">
	<p>click <html:submit>here </html:submit>to retrieve details using
	&lt;logic:iterator&gt;</p>
</html:form>

</body>
</html>

OUTPUT:

 

DOWNLOAD+SOURCE CODE

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply