JSTL displaying duplicate values

Framework: Springs
In Controller class
Step 1: Stored values in a hashmap
Step 2: Stored this hashmap inside the master HashMap
Step 3: returned ModelAndView(master hashmap as the model and jsp as the view)
In JSP
Step 4:  Retrived the hashmap inside the master hashmap
Step 5: Displayed the values of the hashmap in combo box(<Select> element)

My issues are as below
First time the values are displayed properly
I clicked the back button went to the earlier screen(Login screen) entered the login values and that brought back to this screen again.
This time I found each value repeating twice
I repeated the same thing again
This time it is displaying each value three times

I tried the following to avoid this
typed hashMap.clear() before setting the values in controller class. But no use

I am pasting the jsp, controller classes and the helperclass.
Could anyone help me comeout of this issue.

//////////////////////
///jsp file
/////////////////////

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@page import="java.util.HashMap;" session="false" %>

<html>
<head>
    <title><fmt:message key="title"/></title>
</head>

<body>
<center><b><H1> Home Page </h1></b></center>
	<h1><fmt:message key="heading"/></h1>
      <form action="">
   Name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address: 
   
   Person: <select name="Person">
			    <!--  
			    <option value="Business" label="lblBusiness"/>
			    <option value="School" label="lblSchool">
			    -->		    
			    <c:forEach items="${mapper.names}" var="names">
        			<option value="${names.key}">${names.value}</option>
    			</c:forEach>
			</select>&nbsp;&nbsp;<br/><br/><br/><br/>
 Address Category: 
			<select name="AddressType">
			    <c:forEach items="${mapper.addrTypes}" var="addrTypes">
        			<option value="${addrTypes.key}">${addrTypes.value}</option>
    			</c:forEach>
			</select>
<br />
<br />
<input type="submit" value="Show Address in PDF">&nbsp;&nbsp;
    <input type="submit" value="Add New Address">&nbsp;&nbsp;
    <input type="submit" value="Show Address">
      </form>

</body>
</html>



////////////////////////
///Controller class
///////////////////////

package com.addrbk.ctrl;

import java.util.ArrayList;
import java.util.HashMap;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.addrbk.domain.Login;
import com.addrbk.domain.UserBean;
import com.addrbk.util.AddressBookUtil;


public class LogonController extends SimpleFormController{
	protected final Log logger = LogFactory.getLog(getClass());
	
	ArrayList namesList=null;
	ArrayList addressTypes=null;
	public Object formBackingObject(HttpServletRequest request) throws ServletException
	{
		Login backingObject = new Login();
		logger.info("<<< formBackingObject >>>");
		
		/* The backing object should be set up here, with data for the initial values
		* of the form’s fields. This could either be hard-coded, or retrieved from a
		* database.
		*/
		return backingObject;
	}
//HttpServletRequest request, HttpServletResponse response, Object command, BindException errors
	/*public ModelAndView onSubmit(Object command) throws ServletException 
	{
		
		Login user = (Login)command;
		logger.info("Login form Submitted");
		logger.info("Username >>> :"+user.getUserid());
		logger.info("Password >>> :"+user.getPasswd());
		//Now you can validate to database
		return new ModelAndView("homepage");
	}
	*/
	
	public ModelAndView onSubmit(HttpServletRequest request, 
			HttpServletResponse response, 
			Object command, 
			BindException errors) throws ServletException 
	{
		Login user = (Login)command;
		namesList=AddressBookUtil.getNames();
		addressTypes=AddressBookUtil.getAddressTypes();

		logger.info("Login form Submitted");
		logger.info("Username from request object*** : "+request.getParameter("userid"));
		logger.info("Password from request object*** : "+request.getParameter("passwd"));
		logger.info("Username >>> :"+user.getUserid());
		logger.info("Password >>> :"+user.getPasswd());
		HashMap hmap = new HashMap();
		HashMap names = new HashMap();
		HashMap addrTypes = new HashMap();
		
		for(int i=0; i < namesList.size();i++)
		{
			names.put(Integer.valueOf(i), namesList.get(i));
		}
		for(int i=0; i < addressTypes.size();i++)
		{
			addrTypes.put(Integer.valueOf(i), addressTypes.get(i));
		}
		hmap.clear();
		hmap.put("userid", request.getParameter("userid"));
		hmap.put("names", names);
		hmap.put("addrTypes", addrTypes);
		//Now you can validate to database
		return new ModelAndView("homepage","mapper",hmap);
	}
}

/////////////////////
///AddressBookUtil
////////////////////

package com.addrbk.util;

import java.util.ArrayList;

public class AddressBookUtil {
	static String[] addressTypes = {"PERSONAL","OFFICIAL","BUSINESS","RELATIONS","WORK"};
	static String[] names={"MyName","HisName","HerName"};
	static ArrayList<String> addrTypes = new ArrayList<String>(); 
	static ArrayList<String> namesList = new ArrayList<String>();
	
	public static ArrayList<String> getAddressTypes()
	{
		int index=0;
		while(index < addressTypes.length)
		{
			addrTypes.add(addressTypes[index]);
			index++;
		}
		return addrTypes;
	}
	
	public static ArrayList<String> getNames()
	{
		int index=0;
		while(index < names.length)
		{
			namesList.add(names[index]);
			index++;
		}
		return namesList;
	}
}

Open in new window

prsubjectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
every time you call getNames() you add the names again to the list (same for address types)

>        static ArrayList addrTypes = new ArrayList();
>        static ArrayList namesList = new ArrayList();

makes those local vars instead of member vars
0
Sathish David Kumar NArchitectCommented:
is it form loading time right ???


0
Sathish David Kumar NArchitectCommented:
your write the function to call in the onsubmit function so ur page will call ur onsubmit method in ur submiting time only !!

ArrayList addrTypes = null;
ArrayList namesList = null; //globel defintion ,remove static !!!

define in local
addrTypes = new ArrayList();



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
siddagrlCommented:
solution of both "objects" & "dravidnsr" are right...
But you can optimize it by populating the both ArrayList in static block!!! (instead of populating each time)

Attached is the updated AddressBookUtil.java
public class AddressBookUtil {
        static String[] addressTypes = {"PERSONAL","OFFICIAL","BUSINESS","RELATIONS","WORK"};
        static String[] names={"MyName","HisName","HerName"};
        static ArrayList<String> addrTypes = new ArrayList<String>(); 
        static ArrayList<String> namesList = new ArrayList<String>();
        static {
             int index=0;

             while(index < addressTypes.length)
             {
                     addrTypes.add(addressTypes[index]);
                     index++;
             }

             index=0;
             while(index < names.length)
             {
                     namesList.add(names[index]);
                     index++;
             }
        }
        
        public static ArrayList<String> getAddressTypes()
        {
             return addrTypes;
        }
        
        public static ArrayList<String> getNames()
        {
             return namesList;
        }
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.