Announcement Announcement Module
No announcement yet.
display an arraylist of hashmaps in View Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • display an arraylist of hashmaps in View

    This is probably more of a JSTL question, however I have spent hours debugging and searching online , nothing seems to work..hopefully someone out here could help me with this

    I am trying to iterate over an arraylist(newList) which contains a hashmap using jstl. I am using jstl 1.2. The arraylist is populated before being returned to the view. Heres my onSubmit method of my FormController

    public ModelAndView onSubmit(Object command,
    				BindException errors) throws Exception{
    	        String deviceName = "Site 103";
    	        List<Map> data = new ArrayList<Map>();
    		Map testData = new HashMap();
    		testData.put("time","2008-09-27 11:30:00");
    	        ModelAndView mav = new ModelAndView(new 
                    return mav; 	        
    Following is my code for jsp view

    <center><b> <c:out value="${param['name']}"/></b></center>
    <c:forEach items="${paramValues['data']}" var="record" varStatus = "status">               
      <c:forEach var="val" items="${record.value}">
         <c:out value="${record.key}"/> <c:out value="${val}"/>
    The name shows up , but the list with the hashmap doesnt.
    I have tried different ways, but I am probably not using the right syntax here.
    Whats frustrating is , in one of my other views, I am using the following jstl
    to display map values in a list and that works fine , but the same syntax doesnt work for the view above

    Following is the code for my other view and my other controller
    <c:forEach items="${}" var="record" varStatus = "status">
                        <c:out value="${record.time}" />
                        <c:out value="${record.frequency}" />
     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            Map<String, Object> myModel = new HashMap<String, Object>();
            List<Map> data = new ArrayList<Map>();
    	Map testData = new HashMap();
    	testData.put("time","2008-09-27 11:30:00");
            return new ModelAndView("menu", "model", myModel);
    Thanks for looking into this!!

  • #2
    Really, really, really a longshot... but could the first (not working) declaration
    Map testData = new HashMap();
    and the second (working)
    Map<String, Object> myModel = new HashMap<String, Object>();
    be of significance?


    • #3
      still no data on the view &lt;&gt;

      Thanks kantorn...

      i did replace that , but i m still not getting any data on the view...

      I think its something to do with the way my ModelAndView is defined.
      I have done some testing and have realised that if I dont use the RedirectView Constructor for my ModelAndViewObject, I am able to iterate using
      <modelname.objectname> = <$>. However I cant do that for some reason, if I return the view using RedirectView.

      In my case, I have some referencedata that is displayed when the form first loads. On submit I want to display the same input form and referencedata along with the hashmap data in a table on the same page. Hence I am using

       ModelAndView mav1 = new ModelAndView(new RedirectView(getSuccessView()));
      I also tried using
       ModelAndView mav1 = new ModelAndView(new RedirectView(getSuccessView()),"model",model);
      and then in the jstl using $ to get the list of map...But it doesnt work...

      Any suggestions!!???


      • #4
        From the reference manual:
        The RedirectView simply ends up issuing an HttpServletResponse.sendRedirect() call, which will come back to the client browser as an HTTP redirect. All model attributes are simply exposed as HTTP query parameters. This does mean that the model must contain only objects (generally Strings or convertible to Strings) which can be readily converted to a string-form HTTP query parameter.
        My guess is that a Map isn't something that Spring is able to readily convert to query parameters so you are not getting this information passed through.


        • #5
          how would i load the same form??

          Thanks Cmelgar...

          That does explain a lot. So how would you recommend I return the Modelandview so that the same form page is loaded with the map values. I also have some referencedata that I would like to be displayed as well after the form is submitted. Instead of Redirectview(getSuccessView()), I tried using the following

          ModelAndView mav = new ModelAndView("menu");
          This throws an exception as follows:
          javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.jsp.JspException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'menu' available as request attribute
          My servlet-mapping file is as follows
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns=""
            <!-- the application context definition for the DispatcherServlet -->
           <!--  <bean id="messageSource" class="">
                  <property name="basename" value="messages"/>
              </bean> -->
            <bean name="/menu.htm" class="com.controllers.MenuController">
                   <property name="sessionForm" value="true"/>
                  <property name="commandName" value="menu" />
                  <property name="formView" value="menu"/>        
                  <property name="commandClass" value="com.util.Menu"/>
                  <property name="successView" value="menu.htm"/>     
             <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
                  <property name="prefix" value="/WEB-INF/jsp/"></property>
                  <property name="suffix" value=".jsp"></property>        
          What are your suggestions??


          • #6
            Got it to work with showForm

            Well I finally manged to get it to calling "showForm" from onSubmit

            Posting my new code here so that others may benefit.

            Controller code
            ******************************* ********
            protected ModelAndView onSubmit(
                        HttpServletRequest request,
                        HttpServletResponse response, 
                        Object command,
                        BindException errors)
                        throws Exception {
            	String startTime = ((Menu)command).getStartTime();
            	String endTime = ((Menu)command).getEndTime();       
            	String siteNo = ((Menu)command).getSiteNo();
            	String output = "For Site: " + siteNo;
   "StartTime is " + startTime + "EndTime is :" + endTime + "SIte No is " + siteNo); 
            	Map model = errors.getModel();		      
            	List<Map> data = DataManager.getData(siteNo, startTime, endTime);
            	// Redirect back to form with new model
            	// This will ensure referenceData is set as well.			
            	return showForm(request, response, errors,model);    
            Following is my jsp code
            <c:forEach items="${data}" var="record" varStatus = "status">
               <tr><td width="200" ><c:out value="${record.time}" /> </td>
               <td width="200"><c:out value="${record.frequency}" /></td>
                <td width="200"><c:out value="${}" /></td></tr>
            And it works

            I guess the new RedirectView() was the culprit here...
            Thanks for all your help !!