Friday, 19 December 2008

Hints for a complete STRUTS Application (2)

Hints for a STRUTS Application
Source code is available from arsaral(at)

2nd Hint: A very simple Struts Iterator Tag example

This program uses the navigation trick of the 1st HINT to prepare the simple list that will be iterated to be printed on the screen.


<logic:forward name="setList"></logic:forward>

<forward name="setList" path="/" />

<action name="setList" path="/setList" type="com.actions.SetListAction">
<forward name="success" path="/iterateList.jsp"></forward>

As seen, first you will run index.jsp than index.jsp will run and that will run

package com.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SetListAction extends Action {

// The constructor method for this class
public SetListAction() {

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();

java.util.ArrayList list = new java.util.ArrayList();
list.add("item 1");
list.add("item 2");
list.add("item 3");


ActionForward forward = mapping.findForward("success");
return forward;
As seen above, prepares a list named baseList and then forwards the action with “success” to struts-config.xml. struts-config.xml than runs iterateList.jsp which is given below.

<logic:present name="baseList">
<logic:iterate id="iteratorItem" name="baseList">
Item Val: <bean:write name="iteratorItem" /><br />
<html:submit value="Continue"/>

iterateList.jsp very simply writes the items of the list to the screen.