Announcement Announcement Module
Collapse
No announcement yet.
display an arraylist of hashmaps in View Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

    Code:
    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");
    		testData.put("frequency","143"); 
    		data.add(testData);      
    	        ModelAndView mav = new ModelAndView(new 
                        RedirectView(getSuccessView()));       
    	        mav.addObject("data",data);
    	        mav.addObject("name",deviceName); 
    	        logger.info(mav.toString());	                   
                    return mav; 	        
    	        
    	    }
    Following is my code for jsp view

    Code:
    <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}"/>
      </c:forEach>           
    </c:forEach>
    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
    Code:
    <c:forEach items="${model.data}" var="record" varStatus = "status">
                        <c:out value="${record.time}" />
                        <c:out value="${record.frequency}" />
    </c:forEach>
    Code:
     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");
    	testData.put("frequency","143"); 
    	data.add(testData);        
            myModel.put("data",data);
            return new ModelAndView("menu", "model", myModel);
    
        }
    Thanks for looking into this!!

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

    Comment


    • #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> = <$model.data>. 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

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

      Any suggestions!!???

      Comment


      • #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.

        Comment


        • #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

          Code:
          ModelAndView mav = new ModelAndView("menu");
          This throws an exception as follows:
          Code:
          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
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          
          <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          
            <!-- the application context definition for the DispatcherServlet -->
            
            
           <!--  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
                  <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>
            
             
             <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>        
              </bean>
            
          
          </beans>
          What are your suggestions??

          Comment


          • #6
            Got it to work with showForm

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

            Posting my new code here so that others may benefit.

            Controller code
            ******************MenuController.java************* ********
            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;
            	logger.info("StartTime is " + startTime + "EndTime is :" + endTime + "SIte No is " + siteNo); 
            	Map model = errors.getModel();		      
            	List<Map> data = DataManager.getData(siteNo, startTime, endTime);
            	model.put("data",data);
            	model.put("newoutput",output);
            			
            	// 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
            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="${record.name}" /></td></tr>
            </c:forEach>
            And it works fine...now

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

            Comment

            Working...
            X