Using Logic Tags to Iterate over data and View on JSP Page

Using Logic Tags to Iterate over data and View on JSP Page

Previous Home Next

 

An Iterator is public interface in Java collections framework used iterate over a collection. Struts provide logic tags that enable you to have display logic in your view without putting Java code on JSP pages.

 
Here We have following steps which explain way to use logic tag to display on JSP. 

Directory Structure of IteratorLogicTagExample in Struts 1.3 Using MyEclipse IDE




1. Create a JavaBean :-  First step create a java bean class.This have getter and setter method.

IteratorForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class IteratorForm extends ActionForm {
	private String[] nameList=new String[5];
	public String[] getNameList() {
		nameList[0]="Rituraj Tyagi";
		nameList[1]="Sachin Tyagi";
		nameList[2]="Rajesh Patel";
		nameList[3]="Harsh Singh";
		nameList[4]="Sumaro Ram";
		return nameList;
	}
	public void setNameList(String[] nameList) {
		this.nameList = nameList;
	}
}

2. Create a new Action called IteratorActionAction :-

package org.r4r.struts;

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 IteratorAction extends Action {
	public ActionForward execute(ActionMapping mapping,
		ActionForm form,HttpServletRequest request,HttpServletResponse response) 
	    throws Exception{
		return mapping.findForward("success");
	}
}

3. Create ApplicationResources properties file:- 

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_IteratorLogicTagExample
label.title=Struts 1.3 Logic iterate tag Example
label.header=Struts 1.3 Logic iterate tag Example

4.Create web.xml file map the ActionServlet:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

5.Create strutd-config.xml file and configur the bean class , action class and properties class:- 

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans >
  <form-bean name="iteratorForm" type="org.r4r.struts.IteratorForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  <action path="/iteratorAction" name="iteratorForm" type="org.r4r.struts.IteratorAction">
  <forward name="success" path="/success.jsp"/>
  </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

6.Create a new JSP called index.jsp.

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<html:link action="/iteratorAction">
Logic Iterator Tag Example
</html:link>
</body>
</html>

7.In the new JSP Import the logic tag library into the success .jsp.

<%@ taglib uri="/tags/struts-logic" prefix="logic"%> 

8.Check to see if the users are present in scope with the tag. 

9.Iterate through user scope. 

10.For each iteration, print out the success.jsp page.
 
success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<h4><logic:iterate id="iterateTag" name="iteratorForm" property="nameList">
<ul><li><%= iterateTag %></li></ul>
</logic:iterate>
</h4>
</body>
</html>

Output



Previous Home Next