Link to home
Start Free TrialLog in
Avatar of prsubject
prsubjectFlag for India

asked on

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

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is it form loading time right ???


ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial